[英]Unmarshal XML tags with same name using JAXB
我遇到了一個棘手的問題,我應該使用JAXB庫解組XML輸入流,除了XML結構沒有幫助。
我的問題: item
標簽用於simple element with a value
的simple element with a value
,或用於list of other "items"
的list of other "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>
當然,該數據可能會稍微復雜一些與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標簽可能是列表或元素。
我甚至嘗試使用正則表達式將該項/列表標記替換為另一項,但很難找到結束標記...
而且,當然,我無法改變這種結構,這不在我手中。 有人有辦法處理這種結構嗎?
我建議你這個解決方案。 通過這種方式,您可以根據需要添加任意數量的級別。
Root.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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.