Search code examples
javaxmljaxbsaxstax

Child element without root element in java


I have an xml document like this:

<root>
    <device>
        <v1>blah</v1>
    </device>
</root>

I want to parse this document, but just the

    <device>
        <v1>blah</v1>
    </device>

part. I want to ignore the root element. How can I unmarshal this with jaxb?


Solution

  • Assuming that your JAXB definition knows nothing about <root>, i.e. you can't just unmarshal the whole thing and look inside the resulting Root object:

    1. Parse into a Document.
    2. Use XPath / DOM traversal / whatever to get [a] reference[s] to the device Node[s].
    3. Use unmarshaller.unmarshal (node).