0

下面是我的 xml 代码,我需要在其中显示每个节点的属性,即名称和类型。

xml:

<node label="Tree Data">
<node name="View" type="Page">
<node name="Organisational Structure" type="Page"/>
<node name="Experience" type="Page"/>
<node name="Expertise" type="Language">
<node name="Flex" type="Language"/>
<node name="Android" type="Language"/>
<node name="Java" type="Language"/>
</node>
<node name="Project" type="Project">
<node name="Sabre" type="Project"/>
<node name="Android Project" type="Project"/>
<node name="Media Framework" type="Project"/>
<node name="CCD" type="Project"/>
<node name="Redbus" type="Project"/>
<node name="India Today" type="Project"/>
<node name="Money Control" type="Project"/>
</node>
</node>
<node name="Reorganize" type="Action"/>
<node name="Add New Employee" type="Action">
<node name="Dir" type="Page"/>
<node name="P.M" type="Page"/>
<node name="S.E" type="Page"/>
<node name="JUN" type="Page"/>
<node name="EMP" type="Page"/>
</node>
<node name="Add Project Members" type="Action"/>
</node>

我得到以下输出:

输出:

01-23 17:13:26.047: I/System.out(3846): name : 
01-23 17:13:26.057: I/System.out(3846): type : 
01-23 17:13:26.057: I/System.out(3846): name : View
01-23 17:13:26.077: I/System.out(3846): type : Page
01-23 17:13:26.077: I/System.out(3846): name : Organisational Structure
01-23 17:13:26.077: I/System.out(3846): type : Page
01-23 17:13:26.097: I/System.out(3846): name : Experience
01-23 17:13:26.097: I/System.out(3846): type : Page
01-23 17:13:26.097: I/System.out(3846): name : Expertise
01-23 17:13:26.097: I/System.out(3846): type : Language
01-23 17:13:26.097: I/System.out(3846): name : Flex
01-23 17:13:26.097: I/System.out(3846): type : Language
01-23 17:13:26.117: I/System.out(3846): name : Android
01-23 17:13:26.117: I/System.out(3846): type : Language
01-23 17:13:26.117: I/System.out(3846): name : Java
01-23 17:13:26.128: I/System.out(3846): type : Language
01-23 17:13:26.128: I/System.out(3846): name : Project
01-23 17:13:26.139: I/System.out(3846): type : Project
01-23 17:13:26.139: I/System.out(3846): name : Sabre
01-23 17:13:26.139: I/System.out(3846): type : Project
01-23 17:13:26.147: I/System.out(3846): name : Android Project
01-23 17:13:26.147: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : Media Framework
01-23 17:13:26.167: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : CCD
01-23 17:13:26.167: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : Redbus
01-23 17:13:26.167: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : India Today
01-23 17:13:26.177: I/System.out(3846): type : Project
01-23 17:13:26.187: I/System.out(3846): name : Money Control
01-23 17:13:26.187: I/System.out(3846): type : Project
01-23 17:13:26.187: I/System.out(3846): name : Reorganize
01-23 17:13:26.187: I/System.out(3846): type : Action
01-23 17:13:26.187: I/System.out(3846): name : Add New Employee
01-23 17:13:26.187: I/System.out(3846): type : Action
01-23 17:13:26.197: I/System.out(3846): name : Dir
01-23 17:13:26.197: I/System.out(3846): type : Page
01-23 17:13:26.217: I/System.out(3846): name : P.M
01-23 17:13:26.217: I/System.out(3846): type : Page
01-23 17:13:26.217: I/System.out(3846): name : S.E
01-23 17:13:26.217: I/System.out(3846): type : Page
01-23 17:13:26.217: I/System.out(3846): name : JUN
01-23 17:13:26.217: I/System.out(3846): type : Page
01-23 17:13:26.228: I/System.out(3846): name : EMP
01-23 17:13:26.228: I/System.out(3846): type : Page
01-23 17:13:26.237: I/System.out(3846): name : Add Project Members
01-23 17:13:26.237: I/System.out(3846): type : Action

但我实际上需要以下输出:

所需的输出:

01-23 17:13:26.057: I/System.out(3846): name : View
01-23 17:13:26.077: I/System.out(3846): type : Page
01-23 17:13:26.077: I/System.out(3846): name : Organisational Structure
01-23 17:13:26.077: I/System.out(3846): type : Page
01-23 17:13:26.097: I/System.out(3846): name : Experience
01-23 17:13:26.097: I/System.out(3846): type : Page
01-23 17:13:26.097: I/System.out(3846): name : Expertise
01-23 17:13:26.097: I/System.out(3846): type : Language
01-23 17:13:26.097: I/System.out(3846): name : Flex
01-23 17:13:26.097: I/System.out(3846): type : Language
01-23 17:13:26.117: I/System.out(3846): name : Android
01-23 17:13:26.117: I/System.out(3846): type : Language
01-23 17:13:26.117: I/System.out(3846): name : Java
01-23 17:13:26.128: I/System.out(3846): type : Language
01-23 17:13:26.128: I/System.out(3846): name : Project
01-23 17:13:26.139: I/System.out(3846): type : Project
01-23 17:13:26.139: I/System.out(3846): name : Sabre
01-23 17:13:26.139: I/System.out(3846): type : Project
01-23 17:13:26.147: I/System.out(3846): name : Android Project
01-23 17:13:26.147: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : Media Framework
01-23 17:13:26.167: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : CCD
01-23 17:13:26.167: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : Redbus
01-23 17:13:26.167: I/System.out(3846): type : Project
01-23 17:13:26.167: I/System.out(3846): name : India Today
01-23 17:13:26.177: I/System.out(3846): type : Project
01-23 17:13:26.187: I/System.out(3846): name : Money Control
01-23 17:13:26.187: I/System.out(3846): type : Project
01-23 17:13:26.187: I/System.out(3846): name : Reorganize
01-23 17:13:26.187: I/System.out(3846): type : Action
01-23 17:13:26.187: I/System.out(3846): name : Add New Employee
01-23 17:13:26.187: I/System.out(3846): type : Action
01-23 17:13:26.197: I/System.out(3846): name : Dir
01-23 17:13:26.197: I/System.out(3846): type : Page
01-23 17:13:26.217: I/System.out(3846): name : P.M
01-23 17:13:26.217: I/System.out(3846): type : Page
01-23 17:13:26.217: I/System.out(3846): name : S.E
01-23 17:13:26.217: I/System.out(3846): type : Page
01-23 17:13:26.217: I/System.out(3846): name : JUN
01-23 17:13:26.217: I/System.out(3846): type : Page
01-23 17:13:26.228: I/System.out(3846): name : EMP
01-23 17:13:26.228: I/System.out(3846): type : Page
01-23 17:13:26.237: I/System.out(3846): name : Add Project Members
01-23 17:13:26.237: I/System.out(3846): type : Action

爪哇代码:

package com.example.xmlparsevdatr;

import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    Document dom;

    public MainActivity() {
        MainActivity();

        ArrayList<Element> listGroup = getGroups();

    }

    private ArrayList getGroups() {
        try {
            Element documentElement = (Element) dom.getDocumentElement();
            NodeList childNodes = documentElement.getElementsByTagName("node");

            for (int i = 0; i < childNodes.getLength(); i++) {

                if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) childNodes.item(i);
                    String name = element.getAttribute("name");
                    String type = element.getAttribute("type");
                    System.out.println("name : " + name);
                    System.out.println("type : " + type);
                }
            }
        } catch (Exception e) {
        }
        return null;

    }

    private void MainActivity() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // System.out.println("Inside xml");

        try {
            String sURL = "http://192.168.2.6/cgi-bin/EmployeeTreeCgi/TreeStrutRet.cgi?Username=admin@ohile.com";
            URL url = new URL(sURL);
            DocumentBuilder db = dbf.newDocumentBuilder();

            dom = db.parse(new InputSource(url.openStream()));

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
4

2 回答 2

1

问题是您正在尝试打印不存在的第一个节点的名称和类型。因此,您会在输出中获得前两行。

尝试在打印之前检查元素是否确实具有名称和类型。

于 2013-01-23T11:57:11.710 回答
0

您的 XML 似乎不太好。

first <node>元素does notnametype属性。

理想情况下,在使用 XML 时,您不应该让父项和子项具有相同的名称。例如,XML 应该是这样的:

<books>
    <book id=1/>
    <book id=2/>
</books>

另外,请适当考虑formatting your XML以获得更好的可读性。

编辑:

在检索任何属性之前可以进行检查:

if(childNodes.item(i).hasAttribute("name")) {
    // print the name
}
于 2013-01-23T12:08:17.437 回答