使用JAXB解组具有相同名称的XML标记

时间:2014-12-11 13:06:03

标签: java xml jaxb xsd

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

我的问题:item标记用于simple element with a valuelist 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的{​​{1}}数据可能会更复杂一些...... 所以,例如,我需要能够解码这样的东西:

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个值。

我已经尝试过一些东西,但是无法正确编写Java类来解码它。我不知道是否有可能告诉Jaxb标签可能是列表或元素。

我甚至尝试使用正则表达式将此项/列表标记替换为另一项,但很难找到结束标记...

当然,我无法改变这种结构,这不在我手中。 有人有办法处理这种结构吗?

1 个答案:

答案 0 :(得分:8)

我建议你这个解决方案。通过这种方式,您可以根据需要添加任意数量的级别。

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