我陷入了一个棘手的问题,我应该使用JAXB库取消marshaX输入流,除了XML结构没有帮助。
我的问题是:item
标记用于值为>的简单元素,或用于其他“items”
的列表。
下面是一个简单的XML:
<root>
<item label="This is a LIST item" type="list">
<item label="This is a VALUE item" type="string">Value</item>
</item>
</root>
当然,数据可能会更复杂一点,
项目
包含项目
包含项目
…因此,例如,我需要能够解码如下内容:
<root>
<item label="This is a LIST item" type="list">
<item label="Upper" type="string">ABC</item>
<item label="Lower" type="string">abc</item>
<item num="1" type="list">
<item label="a" type="string">aaaaa</item>
<item label="b" type="string">bbbbb</item>
</item>
<item num="2" type="list">
<item label="a" type="other">0x001</item>
<item label="b" type="string">AbCdEf</item>
<item label="c" type="string">123456</item>
</item>
</item>
</root>
唯一告诉我
项目
是一个列表,是它的type
属性,它将始终具有"list"
一个值。
我尝试了一些事情,但无法成功编写正确的Java类来解码它。我不知道这是否可以告诉Jaxb标签可能是列表或元素。
我甚至尝试对XML进行正则表达式,将此项目/列表标记替换为另一个项目,但很难找到结束标记……
当然,我不能改变这个结构,这不在我的手中。有人有办法处理这种结构吗?
我建议你这个解决方案。通过这种方式,您可以根据需要添加任意数量的级别。
根.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "root", propOrder = {
"items"
})
@XmlRootElement(name = "root")
public class Root
implements Serializable
{
private final static long serialVersionUID = 1234567890L;
@XmlElement(name = "item")
protected List<Item> items;
/**
* Gets the value of the items property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the items property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getItems().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Item }
*
*
*/
public List<Item> getItems() {
if (items == null) {
items = new ArrayList<Item>();
}
return this.items;
}
}
Item.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "item", propOrder = {
"content"
})
@XmlRootElement(name = "item")
public class Item
implements Serializable
{
private final static long serialVersionUID = 1234567890L;
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
@XmlAttribute(name = "num")
protected String num;
@XmlAttribute(name = "label")
protected String label;
@XmlAttribute(name = "type")
protected String type;
/**
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
* {@link Object }
*
*
*/
public List<Object> getContent() {
if (content == null) {
content = new ArrayList<Object>();
}
return this.content;
}
/**
* Recupera il valore della proprietà num.
*
* @return
* possible object is
* {@link String }
*
*/
public String getNum() {
return num;
}
/**
* Imposta il valore della proprietà num.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setNum(String value) {
this.num = value;
}
/**
* Recupera il valore della proprietà label.
*
* @return
* possible object is
* {@link String }
*
*/
public String getLabel() {
return label;
}
/**
* Imposta il valore della proprietà label.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setLabel(String value) {
this.label = value;
}
/**
* Recupera il valore della proprietà type.
*
* @return
* possible object is
* {@link String }
*
*/
public String getType() {
return type;
}
/**
* Imposta il valore della proprietà type.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setType(String value) {
this.type = value;
}
}
我用过这个XSD
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root" />
<xs:complexType name="root">
<xs:sequence>
<xs:element name="item" type="item" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:element name="item" type="item" />
<xs:complexType name="item" mixed="true">
<xs:sequence>
<xs:any maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute type="xs:string" name="num" use="optional" />
<xs:attribute type="xs:string" name="label" use="optional" />
<xs:attribute type="xs:string" name="type" use="optional" />
</xs:complexType>
</xs:schema>
Main.java
public static void main(String[] args) throws Throwable {
JAXBContext jc = JAXBContext.newInstance(Root.class, Item.class);
Root r = new Root();
Item i = new Item();
i.setLabel("This is a LIST item");
i.setType("List");
Item i2 = new Item();
i2.setLabel("Upper");
i2.setType("string");
i2.getContent().add("ABC");
i.getContent().add(i2);
Item i3 = new Item();
i3.setLabel("Lower");
i3.setType("string");
i3.getContent().add("abc");
i.getContent().add(i3);
Item i4 = new Item();
i4.setNum("1");
i4.setType("list");
Item i5 = new Item();
i5.setLabel("a");
i5.setType("other");
i5.getContent().add("aaaaa");
i4.getContent().add(i5);
i.getContent().add(i4);
r.getItems().add(i);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.marshal(r, System.out);
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<item label="This is a LIST item" type="List">
<item label="Upper" type="string">ABC</item>
<item label="Lower" type="string">abc</item>
<item num="1" type="list">
<item label="a" type="other">aaaaa</item>
</item>
</item>
</root>
我正在尝试解组一个包含多个同名元素的XML文档。我不确定是否需要创建bean的Arraylist并将其传递给解组器。我希望有人能给我一些建议来解决这个问题。我试图解析的XML是一个SOAP响应,但我去掉了信封,所以我只有它的主体,它看起来是这样的: 这是从一个包含50多个字段的表返回的,但我创建了一个testBean,并且我定义了fkdevice只是为了使其简单,我的bean看起来像这样: 这给了
我有这种结构的xml文件: 有没有可能将这种XML解组到一个对象中。问题是每个元素都有一些独特的标签。我想把这些值放到列表中,但是我不知道怎么做。
我已经生成java类使用从一个xsd,其中根元素是类型的。 jaxb生成的根元素是 当我尝试解组与该xsd对应的xml并强制转换JaxbElement时,它会引发一个强制转换异常: 片段:
我正在尝试使用JAXB解组一个xml文件。当我使用具有正确名称和命名空间的@XmlElement时,解组工作(例如@XmlElement(name=“name”,namespace=”http://www.test.com")) xml文件(test.xml)的内容: JAXBExample.java的内容是: 内容 包装stackoverflow.problem.jaxb.ns; 如果我取消注释
我从 3 个不同的客户端收到 3 个不同的 xml 输入: 来自客户端1的输入1:(命名空间前缀:,命名空间url:) 来自客户端 2 的输入 2:(命名空间前缀:,命名空间 URL:) 从客户端3输入3:(名称空间前缀: 时,< li >调用< code>String.replace() 这让我想到这种变量命名空间的方法是否应该完全可以接受。如果有什么可以改进此代码的方法,那么我要求社区在答案/
我认为这对真正理解JAXB绑定文件的人来说很容易... 如何配置JAXB将多个元素解组到同一个类中? 注意:我想避免在我的项目中添加另一个依赖项(比如MOXy)。理想情况下,这可以通过注释或自定义绑定文件来实现。 我有一个XML文档,其中包含许多相同元素的变体——每个元素都有完全相同的属性。使用下面的示例,我只关心“员工”,但XML指定了“董事、经理和员工”。为了我们的目的,这些都是同一个父类的子