简体   繁体   English

JAXB XML unmarshalling只能看到根元素

[英]JAXB XML unmarshalling only sees the root element

I have a problem unmarshalling a rather simple XML-document to plain Java objects. 我有一个问题是将一个相当简单的XML文档解组为纯Java对象。

This is what my XML looks like: 这就是我的XML的样子:

<?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>

It's part of a much bigger file, a specification of the Hl7v3 code system for representing relationships between persons. 它是更大文件的一部分,是用于表示人与人之间关系的Hl7v3代码系统的规范。

I created two Java classes for the CodeSystem and Code elements: 我为CodeSystem和Code元素创建了两个Java类:

@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 a package-info.java containing: 我添加了一个package-info.java,其中包含:

@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 straightforward: 解组是非常简单的:

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

This however results in an empty CodeSystem object. 但是这会导致一个空的CodeSystem对象。 Nothing except the root element is parsed from the XML. 除了根元素之外,没有任何内容从XML中解析出来。

I can't figure out why the name, desc and code elements aren't recognized. 我无法弄清楚为什么名称,desc和代码元素无法识别。 Do they reside in a different namespace then the root element? 它们是否位于与根元素不同的命名空间中? They shouldn't be, because the namespace declaration in the root element is unprefixed. 它们不应该是,因为根元素中的名称空间声明没有前缀。

What am I missing? 我错过了什么?

In your XML document you have specified a default namespace. 在XML文档中,您指定了一个默认命名空间。 This means that any non-prefixed element is going to be in that namespace. 这意味着任何非前缀元素都将位于该命名空间中。 In the fragment below both the codeSystem and name elements are qualified with the urn:hl7-org:v3 namespace. 在下面的片段中, codeSystemname元素都使用urn:hl7-org:v3命名空间进行限定。

<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 on the @XmlSchema annotation to XmlNsForm.QUALIFIED . 您只需@XmlSchema注释上的elementFormDefault属性更改为XmlNsForm.QUALIFIED You currently have it as XmlNsForm.UNQUALIFIED which means that only global elements will be namespace qualified (the one corresponding to @XmlRootElement in your use case. 您目前将它作为XmlNsForm.UNQUALIFIED表示,这意味着只有全局元素将是命名空间限定的(在您的用例中对应于@XmlRootElement元素)。

@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;

For More Information 欲获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM