Bongo
Bongo

Reputation: 384

Extracting attribute value in XML using MOXy

Does anyone know how I can map the value of an attribute to a field. Say the value of tag lang, which is "sw" to a field in a class

<book category="cooking">
        <title lang="sw">Vegetarian</title>
        <year>2008</year>
        <price>30.00</price>
        <authors>
            <author>Tichaona</author>
            <author>Ngodza</author>
        </authors>
    </book>

......
......
......

<book category="Huffman Coding">
        <title lang="en">Encryption</title>
        <year>2000</year>
        <price>45.00</price>
        <authors>
            <author>Ruvimbo</author>
        </authors>
    </book>

I want to be able to put the value in attribute lang to a field using MOXy extension. More along the lines of

@XmlPath("title/@lang")
private String language;

Can anyone help

Upvotes: 0

Views: 115

Answers (1)

Christy
Christy

Reputation: 199

Assuming you have Moxy set up correctly, I think the issue you would have with your example is that the Xpath "title/@lang" would not actually resolve to the attribute you are wanting.

Something like //title/@lang should do the trick for picking the value of the lang attribute.

Upvotes: 2

Related Questions