使用 java (DOM Parser) 解析多级 XML 文件 [英] Parsing the multilevel XML File using java (DOM Parser)

查看:35
本文介绍了使用 java (DOM Parser) 解析多级 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 XML 文件的示例:

Here is example of my XML file :

    ?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="xslt/options.xsl"?>
    <options>
      <version>0001</version>
      <title>ConfigData</title>
      <category>
        <name>GConfigData</name>
        <option>
          <name>String_name</name>
          <value>350.16.01a</value>
          <control>
            <type>TextBox2</type>
            <caption> String Name</caption>
            <left>0</left>
            <top>0</top>
            <width>2600</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>FileID</name>
          <value>1601</value>
          <control>
            <type>TextBox2</type>
            <caption>file version</caption>
            <left>0</left>
            <top>900</top>
            <width>2600</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>systemID</name>
          <value>0</value>
          <control>
            <type>TextBox2</type>
            <caption>System ID</caption>
            <left>0</left>
            <top>1800</top>
            <width>2400</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>SyncTime</name>
          <value>2</value>
          <control>
            <type>TextBox2</type>
            <caption>Sync Time</caption>
            <left>0</left>
            <top>2700</top>
            <width>2400</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>UseServer</name>
          <value>0</value>
          <control>
            <type>TextBox2</type>
            <caption>Use Server</caption>
            <left>0</left>
            <top>3600</top>
            <width>2400</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>CommType</name>
          <value>0</value>
          <control>
            <type>FixedList</type>
            <caption>Comm Type</caption>
            <left>0</left>
            <top>4500</top>
            <width>2400</width>
            <height>900</height>
            <list>                                              
              <item>
                <text>Parellel</text>
                <value>0</value>
              </item>
              <item>
                <text>Simple Serial</text>
                <value>1</value>
              </item>
              <item>
                <text>Complex Serial</text>
                <value>2</value>
              </item>
            </list>
          </control>
        </option>
        <option>
          <name>YYBasis</name>
          <value>70</value>
          <control>
            <type>TextBox2</type>
            <caption>Set YY Basis</caption>
            <left>0</left>
            <top>5400</top>
            <width>2400</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>Separator</name>
          <value>46</value>
          <control>
            <type>TextBox2</type>
            <caption>Separator</caption>
            <left>0</left>
            <top>6300</top>
            <width>2400</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>WholeSeparator</name>
          <value>44</value>
          <control>
            <type>TextBox2</type>
            <caption>Whole Separator</caption>
            <left>0</left>
            <top>7200</top>
            <width>2400</width>
            <height>900</height>
            <font>Courier</font>
            <scroll_bar>0</scroll_bar>
          </control>
        </option>
        <option>
          <name>DateFormat</name>
          <value>0</value>
          <control>
            <type>FixedList</type>
            <caption>Date Format</caption>
            <left>2600</left>
            <top>0</top>
            <width>2400</width>
            <height>900</height>
            <list>
              <item>
                <text>MM/DD/YY</text>
                <value>0</value>
              </item>
              <item>
                <text>MM/DD/YYYY</text>
                <value>1</value>
              </item>
              <item>
                <text>DD/MM/YY</text>
                <value>2</value>
              </item>
              <item>
                <text>DD/MM/YYYY</text>
                <value>3</value>
              </item>
              <item>
                <text>YY/MM/DD</text>
                <value>4</value>
              </item>
              <item>
                <text>MM.DD.YY</text>
                <value>6</value>
              </item>
              <item>
                <text>MM.DD.YYYY</text>
                <value>7</value>
              </item>
              <item>
                <text>DD.MM.YY</text>
                <value>8</value>
              </item>
              <item>
                <text>DD.MM.YYYY</text>
                <value>9</value>
              </item>
              <item>
                <text>YY.MM.DD</text>
                <value>10</value>
              </item>
              <item>
                <text>YYYY.MM.DD</text>
                <value>11</value>
              </item>
            </list>
          </control>
        </option>
      </category>
    </options>

我编写了java代码来解析每个选项的名称、标题和值.这是代码:

I wrote the java code to parse the name , caption and value of each option. Here is code :

public class XMLParsingSingleFileFinal {



    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
       {
          //Get Document Builder
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();

          //Build Document
          Document document = builder.parse(new File("options.xml"));

          //Normalize the XML Structure; It's just too important !!
          document.getDocumentElement().normalize();
          XPath xPath =  XPathFactory.newInstance().newXPath();

          //Here comes the root node
          Element root = document.getDocumentElement();
          System.out.println(root.getNodeName());

          //Get all options
          NodeList nList = document.getElementsByTagName("options");
          System.out.println("Total Options = " + nList.getLength());
          System.out.println("TITLE = " + document.getElementsByTagName("title").item(0).getTextContent());
          System.out.println("VERSION = " + document.getElementsByTagName("version").item(0).getTextContent());

          System.out.println("===================================");

          //Get all category
          NodeList nList1 = document.getElementsByTagName("category");
          System.out.println("Total Category inside options = " + nList1.getLength());
          //int count1 = nList1.getLength();


          for (int temp = 0; temp < nList1.getLength(); temp++)
          {
             Node node = nList1.item(temp);
             if (node.getNodeType() == Node.ELEMENT_NODE)
             {
                 Element mElement = (Element) node;
                 System.out.println("\nCategory Name = " + mElement.getElementsByTagName("name").item(0).getTextContent());
                 NodeList nList2 = mElement.getElementsByTagName("option");
                 System.out.println("option inside category = " + nList2.getLength());
                 System.out.println("\n\t");
                // int count = nList2.getLength();


                 for (int temp1 = 0; temp1 < nList2.getLength()/2; temp1++) 
                {

                    Node nNode = nList2.item(temp1);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE)
                    {

                    Element nElement = (Element) nNode;

                 System.out.println("\tOption Name = " + mElement.getElementsByTagName("name").item(temp1+1).getTextContent());
                 System.out.println("\t\tCaption Name = " + mElement.getElementsByTagName("caption").item(temp1).getTextContent());

                 System.out.println("\t\tValue = " + mElement.getElementsByTagName("value").item(temp1).getTextContent());



                 System.out.println("\n\t");

            }

              }  
                 System.out.println("\n\t");
             }   
          }   
       }
}

我的主要目的是解析节点选项"的值".

My main aim is to parse the "value" of the node "option".

如您所见,在选项"- commtype 中,有属性item"也有子节点value".

As you can see that in the "option" - commtype , there is attribute "item" which also have childnode "value".

所以在解析时,直到选项名称commtype"它正在生成正确的数据.继续下一个选项,它从上一个选项中获取子节点项目"的值".

So while parsing , Till the Option name "commtype" it is producing the correct data. Moving on to next option its taking the "value" of childnode "item" from previous option.

Example:(Parse Result)

options
Total Options = 1
TITLE = ConfigData
VERSION = 0001
===================================
Total Category inside options = 23

Category Name = GConfigData
option inside category = 38


    Option Name = String_name
        Caption Name = String Name
        Value = 350.16.01a


    Option Name = FileID
        Caption Name =  file version
        Value = 1601


    Option Name = SystemID
        Caption Name = System ID
        Value = 0


    Option Name = SyncTime
        Caption Name = Sync Time
        Value = 2


    Option Name = UseServer
        Caption Name = Use Server
        Value = 0


    Option Name = CommType
        Caption Name = Comm Type
        Value = 0


    Option Name = YYBasis
        Caption Name = Set YY Basis
        Value = 0        /*(Here the value should be 70 as in XML file , But its taking the value of option(Name:CommType)/control/list/item(text:parellel)/value )*/


    Option Name = Separator
        Caption Name =  Separator
        Value = 1       /*(Here the value should be 46 as in XML file , But its taking the value of option(Name:CommType)/control/list/item(text:simple serial)/value)*/


    Option Name =WholeSeparator
        Caption Name = Whole Separator
        Value = 2     /*(Here the value should be 44 as in XML file , But its taking the value of option(Name:CommType)/control/list/item(text:complex serial)/value)*/


    Option Name = DateFormat
        Caption Name = Date Format
        Value = 70    //(Value should be 0)

Option Name: CommType 之后,每个选项的值解析错误.

After the Option Name: CommType , the value of each option is parsed wrongly.

有什么办法可以解决这个问题?我是 Java 和 XML 的新手.

What can be the solution of this? I am new to java as well as XML.

PS:这是我在这个论坛上的第一个问题.对于任何拼写错误以及提问方式错误,我深表歉意.请尝试以可能的方式帮助我.

PS: This is my first question on this forum.I apologize of any spelling mistake and if the way of questioning is wrong. Please try to help me in possible ways.

推荐答案

不要对节点使用索引\偏移量(硬编码反模式),它会使你的代码不敏捷

Do not use indexes\offset for nodes (hardcoding anti-pattern), it makes your code not agile

SAXReader reader = new SAXReader();
Document document = reader.read(file);
List<Node> nodes = document.selectNodes("/options/category/option");

for (Node node : nodes) {
    System.out.println("caption: " + node.selectSingleNode("control/caption").getText());
    System.out.println("value : " + node.selectSingleNode("value").getText());
}

示例输出(剪切):

caption:  String Name
value : 350.16.01a
caption: file version
value : 1601
caption: System ID
value : 0

需要的依赖:

<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.1.6</version>
</dependency>

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

这篇关于使用 java (DOM Parser) 解析多级 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆