Unmarshal XML tags with the same name using JAXB

I'm stuck on a tricky issue where I have to decouple an XML input stream using the JAXB library, except that the XML structure doesn't help.

My problem: the tag is being item

used for simple element with a value

or for list of other "items"

.

Here's a simple XML:

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

      

Of course, the data can be a little more complex if it items

contains items

containing items

... So, for example, I need to be able to decode something like this:

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

      

The only thing that tells me what item

is a list is its attribute type

, which will always "list"

matter.

I've tried several things, but can't seem to write the Java class correctly to decode it. I don't know if one can even tell Jaxb that a tag can be a list or an element.

I even tried renaming the XML to replace that item / list tag with a different item, but it's hard to find the end tag ...

And, of course, I cannot change this structure, it is not in my hands. Does anyone have a way to deal with this structure?

+3


source to share


1 answer


I offer you this solution. This way you can add as many levels as you need.

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

}

      

I have used this 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);


}

      

Output

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

      

+8


source







All Articles