I would like to deserialize XML as follows using JAXB in Java:
<?xml version="1.0" encoding="UTF-8"?> <root> <container> inner text that I need <foo attrib="meh"> <bar>value</bar> </foo> </container> </root>
What turns me off is capturing the inner text of <container> : I cannot use both @XmlValue to get the inner text and @XmlElement to capture the foo elements that appear after the inner text. See below for a diagram of what I'm looking for.
import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlValue; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; public class App { private static final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><container>text<foo attrib=\"meh\"><bar>waffles</bar></foo></container></root>"; @XmlRootElement(name = "foo") static class Foo { @XmlAttribute public String attrib; @XmlElement public String bar; } @XmlRootElement(name = "container") static class Container {
source share