以XML格式获取子项的值

时间:2013-06-29 19:44:50

标签: java xml xml-parsing

我有当前的XML文件:

<test>
<class name="something" class="some"> 
<field> 
 <name>something</name> 
 <location>Something.class</location>
 <desc></desc> 
</field>
</class>
</test>

为了解析XML文件,我有以下代码:

    public static void parse(){
       try {
       File xml = new File("testfile.xml");
           DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
           Document document = db.parse(xml);

           NodeList node = document.getChildNodes();
           Element root = (Element) node.item(0);
           NodeList classes = root.getElementsByTagName("class");
           for(int i = 0; i < classes.getLength(); i++){
               Node nodes = classes.item(i);
               Element classElement = (Element) nodes;
               System.out.println("Class name: " + classElement.getAttribute("name"));
               System.out.println("Location: " + classElement.getAttribute("class"));
           }

       } catch (Exception ignored){

       }
}

但我不知道将字段元素的值作为子元素。我只使用以下方法获得元素:

NodeList classes = root.getElementsByTagName("field");

我的问题是,如何在元素字段中获取这些值?

1 个答案:

答案 0 :(得分:1)

您可以像对.getChildNodes()那样在field节点中呼叫root

Node field = root.getElementsByTagName("field").item(0);
NodeList fieldChildren = field.getChildNodes();
for (int i = 0; i < fieldChildren.getLength(); i++) {
    Node n = fieldChildren.item(i);
    System.out.println("Node Type: " + n.getNodeName());
    System.out.println("Node Value: \"" + n.getTextContent()+"\"");
}

输出(适用于您的文件):

Node Type: #text
Node Value: " 
 "
Node Type: name
Node Value: "something"
Node Type: #text
Node Value: " 
 "
Node Type: location
Node Value: "Something.class"
Node Type: #text
Node Value: "
 "
Node Type: desc
Node Value: ""
Node Type: #text
Node Value: " 
"

#text个节点是<name><location><desc>之间的空格。

编辑:

对于多个字段,请考虑以下XML:

<test>
    <class name="something" class="some">
        <field>
            <name>something</name>
            <location>Something.class</location>
            <desc/>
        </field>
    </class>
    <class name="something2" class="some2">
        <field>
            <name>something2</name>
            <location>Something.class2</location>
            <desc>2</desc>
        </field>
        <field>
            <name>something22</name>
            <location>Something.class22</location>
            <desc>22</desc>
        </field>
    </class>
</test>

使用代码:

NodeList fields = root.getElementsByTagName("field");
for (int fi = 0; fi < fields.getLength(); fi++) {
    Node field = fields.item(fi);
    NodeList fieldChildren = field.getChildNodes();
    System.out.println("Processing Field #"+fi+":");
    for (int i = 0; i < fieldChildren.getLength(); i++) {
        Node n = fieldChildren.item(i);
        System.out.println("\tNode Type: " + n.getNodeName());
        System.out.println("\tNode Value: \"" + n.getTextContent()+"\"");
    }
}

输出:

Processing Field #0:
    Node Type: name
    Node Value: "something"
    Node Type: location
    Node Value: "Something.class"
    Node Type: desc
    Node Value: ""
Processing Field #1:
    Node Type: name
    Node Value: "something2"
    Node Type: location
    Node Value: "Something.class2"
    Node Type: desc
    Node Value: "2"
Processing Field #2:
    Node Type: name
    Node Value: "something22"
    Node Type: location
    Node Value: "Something.class22"
    Node Type: desc
    Node Value: "22"