I am trying to unmount this XML:
<FHcomment> <TX>rewriting of file</TX> <tool_id>toolA</tool_id> <tool_vendor>Company</tool_vendor> <tool_version>1.7.36.0</tool_version> </FHcomment>
The schema has already been compiled into JAXB classes, see here:
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "tx", "toolId", "toolVendor", "toolVersion", "userName", "commonProperties", "extensions" }) @XmlRootElement(name = "FHcomment") public class FHcomment { @XmlElement(name = "TX", required = true) protected TX tx; @XmlElement(name = "tool_id", required = true) protected BaseName toolId; @XmlElement(name = "tool_vendor", required = true) protected BaseName toolVendor; @XmlElement(name = "tool_version", required = true) protected BaseVersion toolVersion; @XmlElement(name = "user_name") protected BaseName userName; @XmlElement(name = "common_properties") protected CommonPropertiesType commonProperties; protected ExtensionsType extensions; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); ..... ..... }
My XML decoupling code is as follows:
JAXBContext jc = JAXBContext.newInstance(FHcomment.class); String s = "<FHcomment>....</Fhcomment>"; Unmarshaller unmarshaller = jc.createUnmarshaller(); XMLInputFactory fac = XMLInputFactory.newFactory(); XMLStreamReader xsr = fac.createXMLStreamReader(new StringReader(s)); JAXBElement<FHcomment> foo = unmarshaller.unmarshal(xsr, FHcomment.class); FHcomment val = foo.getValue();
Problem: The resulting FHcomment object does not contain FHcomment children. All are null, which is not the desired result.
How can I tell JAXB to completely untie a given XML into an object?
EDIT: By adding ValidationHandler to Unmsarshaller, I got closer to the problem:
unexpected element (uri: ", local:" TX "). Expected elements: <{htp: //www.example.com/mdf/v4} tool_id>, <{htp: //www.example.com/mdf/ v4} TX>, <{htp: //www.www.example.com/mdf/v4} common_properties>, <{htp: //www.example.com/mdf/v4} tool_version>, <{htp: / /www.example.com/mdf/v4} extensions>, <{OEM: //www.www.example.com/mdf/v4} tool_vendor>, <{HTP: //www.www.example.com/mdf / v4} username>
unexpected element (uri: ", local:" tool_id "). Expected elements: ....
Turns out JAXB doesn't like the fact that the XML provided does not contain namespace information. So how can I tell unmarshaller to ignore namespaces?
EDIT2:
After some more research, I couldn't find a way to trick JAXB into working without checking the namespace. I used the tutorial at http://cooljavablogs.blogspot.de/2008/08/how-to-instruct-jaxb-to-ignore.html to get around my problem. Bad decision, but the best at hand ...