JAXB XML unmarshalling only sees the root element

I have a problem unmarshalling a fairly simple XML document for simple Java objects.

This is what my XML looks like:

<?xml version="1.0" encoding="UTF-8"?> <codeSystem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 vocab.xsd" xmlns="urn:hl7-org:v3"> <name>RoleCode</name> <desc>Codes voor rollen</desc> <code code="SON" codeSystem="2.16.840.1.113883.5.111" displayName="natural sonSon "> <originalText>The player of the role is a male offspring of the scoping entity (parent).</originalText> </code> <code code="DAUC" codeSystem="2.16.840.1.113883.5.111" displayName="Daughter"> <originalText> The player of the role is a female child (of any type) of scoping entity (parent) </originalText> </code> </codeSystem> 

This is part of a much larger file, an Hl7v3 code system specification for representing human relationships.

I created two Java classes for the CodeSystem and Code elements:

 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class CodeSystem { private String name; private String desc; @XmlElement(name = "code") private List<Code> codes; } @XmlAccessorType(XmlAccessType.FIELD) @XmlType public class Code { @XmlAttribute private String code; @XmlAttribute private String codeSystem; @XmlAttribute private String displayName; private String originalText; } 

I added package-info.java containing:

 @XmlSchema( namespace = "urn:hl7-org:v3", elementFormDefault = XmlNsForm.UNQUALIFIED, attributeFormDefault = XmlNsForm.UNQUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(prefix = "", namespaceURI = "urn:hl7-org:v3") } ) package nl.topicuszorg.hl7v3.vocab2enum.model; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema; 

Unmarshalling is pretty simple:

 JAXBContext context = JAXBContext.newInstance(CodeSystem.class); Unmarshaller unmarshaller = context.createUnmarshaller(); CodeSystem codeSystem = (CodeSystem) unmarshaller.unmarshal(new File(args[0])); 

This results in the creation of an empty CodeSystem object. Nothing but the root element is parsed from XML.

I cannot understand why names, descriptors, and code elements are not recognized. Are they in a different namespace and then in the root element? They should not be, because the namespace declaration in the root element has no prefixes.

What am I missing?

+4
source share
1 answer

In your XML document, you specified a default namespace. This means that any non-prefix element will be in this namespace. In the snippet below, the codeSystem and name elements codeSystem qualified using the urn:hl7-org:v3 namespace.

 <codeSystem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 vocab.xsd" xmlns="urn:hl7-org:v3"> <name>RoleCode</name> ... </codeSystem> 

You just need to change the elementFormDefault property in the elementFormDefault annotation to XmlNsForm.QUALIFIED . You currently have it as XmlNsForm.UNQUALIFIED , which means that only global elements will have a namespace matching the @XmlRootElement class in your use case.

 @XmlSchema( namespace = "urn:hl7-org:v3", elementFormDefault = XmlNsForm.QUALIFIED, attributeFormDefault = XmlNsForm.UNQUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(prefix = "", namespaceURI = "urn:hl7-org:v3") } ) package nl.topicuszorg.hl7v3.vocab2enum.model; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema; 

Additional Information

+8
source

All Articles