使用记录类型解析xml

时间:2014-09-12 15:08:19

标签: java xml-parsing jaxb

我有一个xml数据要解析为java Object。但是这个带有记录的xml数据是第一次看到,任何机构都有想法解析它。

<?xml version="1.0" encoding="UTF-8"?>    
<Coder version="1.0">
  <record javaclass="com.dd.Impl">
    <value name="fileName">load</value>
    <record name="load" javaclass="com.wm.dd.Data">
      <value name="@version">1.0</value>
      <record name="lm:Order" javaclass="com.dd.Data">
        <value name="@Id"></value>
        <value name="@UID"></value>
        <value name="@Count">2</value>
        <value name="@Count">0</value>
        <record name="lm:Master" javaclass="com.dd.Data">
          <value name="lm:ID">13</value>
          <value name="lm:Number">382</value>
         </record>
        <array name="wo:Detail" type="record" depth="1">
          <record javaclass="com.dd.Data">
            <value name="lm:ID">13</value>
            <value name="lm:Number">382</value>
            <value name="lm:Code">CD3</value>
            <value name="lm:Occurrence">1</value>               
          </record>
          <record javaclass="com.dd.Data">
            <value name="lm:ID">13</value>
            <value name="lm:Number">382</value>
            <value name="lm:Code">CD2</value>
            <value name="lm:Occurrence">1</value>
          </record>
          <record javaclass="com.dd.Data">
           <value name="lm:ID">13</value>
            <value name="lm:Number">382</value>
            <value name="lm:Code">CD1</value>
            <value name="lm:Occurrence">1</value>
          </record>
         <record javaclass="com.dd.Data">
           <value name="lm:ID">13</value>
            <value name="lm:Number">382</value>
            <value name="lm:Code">CD4</value>
            <value name="lm:Occurrence">1</value>
          </record>              
        </array>
 </record>
    </record>
    <value name="folderPath">dir</value>
    <value name="value">2014</value>
  </record>
</ICoder>

通常,从xml到javaObjects的解析通常使用jaxB代码。

JAXBContext jaxbContext;
            try {
                jaxbContext = JAXBContext.newInstance(OrderType .class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                OrderType order = (OrderType ) jaxbUnmarshaller.unmarshal(file);
                System.out.println(order );
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:1)

  1. 您应该从XML see here
  2. 生成XSD
  3. 如果你使用maven,你可以从XSD生成你的java类

           <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions>
                    <execution>
                        <id>commun-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                            <schemaDirectory>${basedir}/src/main/resources/schema/xsd</schemaDirectory>
                            <strict>true</strict>
                            <extension>true</extension>
                            <verbose>true</verbose>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
  4. 否则请参阅this主题以从xsd

    生成类