gpt4 book ai didi

java - 使用 JAXB 解码具有相同名称的 XML 标签

转载 作者:数据小太阳 更新时间:2023-10-29 02:18:35 25 4
gpt4 key购买 nike

我遇到了一个棘手的问题,我应该使用 JAXB 库解码 XML 输入流,但 XML 结构没有帮助。

我的问题:item 标签用于具有值的简单元素,或用于其他“项目”列表

这是一个简单的 XML:

<root>
<item label="This is a LIST item" type="list">
<item label="This is a VALUE item" type="string">Value</item>
</item>
</root>

当然,数据可能有点复杂,items包含items包含items...因此,例如,我需要能够解码如下内容:

<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>

唯一告诉我 item 是一个列表的是它的 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>

主.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>

关于java - 使用 JAXB 解码具有相同名称的 XML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27423627/

25 4 0