首页 > 解决方案 > jaxb unmarshal 为带有命名空间和递归的 xml 返回空列表

问题描述

我正在尝试使用名称空间解组 xml,但它不断返回 Set 的空列表

xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://profileConfig" xmlns="http://profileConfig"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Profile">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="metadata" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="data" minOccurs="0" maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="ProfileID" type="xs:ID" use="required" />
    </xs:complexType>
</xs:element>

<xs:element name="data">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="data" minOccurs="0" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required" />
        <xs:attribute name="value" type="xs:string" use="optional" />
    </xs:complexType>
</xs:element>

配置文件.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "metadata"
})
@XmlRootElement(name = "Profile")
public class Profile {

    @XmlElement(required = false)
    protected Metadata metadata;
}

元数据.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "data"
})
@XmlRootElement(name = "metadata")
public class MetaData {

    @XmlElement(required = false)
    protected Set<Data> data = new HashSet();
}

数据.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "data"
})
@XmlRootElement(name = "data")
public class Data {

    @XmlAttribute(name="name")
    protected String name = "";

    @XmlElement(required=false)
    protected Set<Data> data = new HashSet();
}

配置文件.xml

<?xml version="1.0" encoding="utf-8"?>
<Profile ProfileID="123456789" xmlns="http://profileConfig">
    <metadata>
        <data name="Type" value="A">
        <data name="Options">
            <data name="Option" value="option1" />
            <data name="Option" value="option2" />
        </data>
    <metadata>
</Profile>

我正在读取 xml 文件并在 java 中解组为 Profile 对象

JAXBContext jaxbContext = JAXBContext.newInstance(Profile.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Profile profile = (Profile) jaxbUnmarshaller.unmarshal(new File("<PATH_TO_XML_FILE>"));

配置文件的 getMetaData() 返回空列表对象,没有任何异常或错误我不确定我到底在哪里犯了错误。

如果我从 xml 中删除命名空间,它会按预期工作。只需使用

<Profile ProfileID="123456789">...</Profile>

更新我尝试过编组,但我认为我的命名空间定义有问题。

Profile profile = new Profile();
Data data = new Data();
data.setName("Type");
data.setValue("A");
MetaData metadata = new MetaData();
metadata.addData(data);
profile.setMetadata(metadata);

JAXBContext jaxbContext = JAXBContext.newInstance(Profile.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(employee, sw);
String xmlContent = sw.toString();
System.out.println(xmlContent);

输出

<ns2:Profile xmlns:ns2="http://profileConfig">
    <ns2:metadata>
        <data name="Type" value="A"/>
    </ns2:metadata>
</ns2:Profile>

如您所见,元素没有附加名称空间,这对我来说很有意义,因为当我尝试获取元数据时,我没有得到 null,而是一个空列表

标签: javaxmlxml-parsingjaxbunmarshalling

解决方案


推荐阅读