繁体   English   中英

JAXB将XML编组到HashMap中

[英]JAXB Unmarshalling of XML into HashMap

我有一个下面的复杂XML结构,需要将其解组:

<abc>
      <pqr>
        <attribute>name</attribute>
        <entry>
          <priorityKey>
            <class>123</class>
            <reason>abc</reason>
          </prioritykey>
          <priority> 1 </priority>
        </entry>

        <entry>
          <prioritykey>
            <class>456</class>
            <reason>abc1</reason>
          </prioritykey>
          <priority>2</priority>
        </entry>
      </pqr>

      <pqr>
       '''
       '''
      </pqr>
    </abc>

abc是根节点。 它可以具有多个pqr元素。 每个pqr元素都有一个属性节点和多个入口节点。 因此,我相信它将是HashMap(entry,attribute)类型。

每个条目依次具有prioritykeypriority ,我相信它们的类型将为HashMap(prioritykey,priority)。

需要解组此xml,但不了解如何配置XMLAdapter

在这里,首先创建合同。 我在学习过程中创建了一个...

<xs:element name="abc" type="abcType" />

<xs:complexType name="abcType">
    <xs:sequence>
        <xs:element name="pqr" type="pqrType" minOccurs="1"
            maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="pqrType">
    <xs:sequence>
        <xs:element name="attribute" type="xs:string" minOccurs="1"
            maxOccurs="1" />
        <xs:element name="entry" type="entryType" minOccurs="1"
            maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="entryType" >
    <xs:sequence>
        <xs:element name="priorityKey" type="priorityKeyType"
            minOccurs="1" maxOccurs="1" />
        <xs:element name="priority" type="xs:int" minOccurs="1"
            maxOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="priorityKeyType">
    <xs:sequence>
        <xs:element name="class" type="xs:int" minOccurs="1"
            maxOccurs="1" />
        <xs:element name="reason" type="xs:string" minOccurs="1"
            maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

将其复制并保存在fileName.xsd文件中。 现在,生成JAXB类或工件。 如果您使用的是IDE,那么这很简单。 单击fileName.xsd文件,您将获得一些生成JAXB工件的选项。 否则,您始终可以使用JAXB下载中存在的lib存在的jaxb-xjc.jar

相同命令java -jar jaxb-xjc.jar fileName.xsd

工件就绪后,您可以使用它们来解组所涉及的xml内容。

暂无
暂无

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

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