Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshall part of XML

I have this XML

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:listItemsResponse xmlns:ns2="http://soap.ws.server.wst.fit.cvut.cz/">
            <return>
                <code>OK</code>
                <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:type="ns2:items">
                    <item>
                        <itemId>1540041177</itemId>
                        <price>5109</price>
                    </item>
                    <item>
                        <itemId>696734629</itemId>
                        <price>5453</price>
                    </item>
                    <item>
                        <itemId>1853843391</itemId>
                        <price>5088</price>
                    </item>
                </data>
            </return>
        </ns2:listItemsResponse>
    </soap:Body>
</soap:Envelope>

and I want to unmarshall the data element to my POJO (not the whole XML, just the part of it). Is it possible with JAXB?

@XmlRootElement(name = "data")
public class Data {

    private List<Item> items;

    public Data() {
    }

    @XmlElement(name = "item")
    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return "Data [" + (items != null ? "items=" + items : "") + "]";
    }

}
like image 460
user219882 Avatar asked Nov 25 '25 19:11

user219882


1 Answers

You can use a StAX XMLStreamReader to advance to the content you are interested in and then unmarshal that.

For More Information

  • http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
like image 127
bdoughan Avatar answered Nov 28 '25 08:11

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!