简体   繁体   English

JAXB将XML编组到HashMap中

[英]JAXB Unmarshalling of XML into HashMap

I have a complex XML Structure as below which needs to be unmarshalled: 我有一个下面的复杂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 is the root node. abc是根节点。 It can have multiple pqr elements. 它可以具有多个pqr元素。 Each pqr element has one attribute node and multiple entry nodes. 每个pqr元素都有一个属性节点和多个入口节点。 So I believe it will be of type HashMap(entry,attribute). 因此,我相信它将是HashMap(entry,attribute)类型。

Each entry in turn has prioritykey and priority which I believe will be of type HashMap (prioritykey,priority). 每个条目依次具有prioritykeypriority ,我相信它们的类型将为HashMap(prioritykey,priority)。

Need to unmarshall this xml but not getting how to configure the XMLAdapter 需要解组此xml,但不了解如何配置XMLAdapter

Here, create the contract first. 在这里,首先创建合同。 I created one as part of my learning process... 我在学习过程中创建了一个...

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

Copy and save this in an fileName.xsd file. 将其复制并保存在fileName.xsd文件中。 Now, generate JAXB classes or artifacts. 现在,生成JAXB类或工件。 If you are using an IDE, then this is simple. 如果您使用的是IDE,那么这很简单。 Click on the fileName.xsd file and you should get some option to generate JAXB artifacts. 单击fileName.xsd文件,您将获得一些生成JAXB工件的选项。 Otherwise, you can always use jaxb-xjc.jar present in the lib present in the JAXB download. 否则,您始终可以使用JAXB下载中存在的lib存在的jaxb-xjc.jar

Command for the same - java -jar jaxb-xjc.jar fileName.xsd . 相同命令java -jar jaxb-xjc.jar fileName.xsd

As the artifacts are in place, you can use them to unmarshall the xml content in question. 工件就绪后,您可以使用它们来解组所涉及的xml内容。

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

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