0

我有当前的 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");

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

4

1 回答 1

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"
于 2013-06-29T19:53:30.543 回答