Java Dom解析xml问题

Java Dom解析xml问题,java,xml,parsing,dom,Java,Xml,Parsing,Dom,我有一个简单的.xml文件,需要对其进行解析。文件如下所示: <table name="agents"> <row name="agent" password="pass" login="agent" ext_uid="133"/> </table> #text null row #text 接下来,我将尝试打印值: document = getDocument(fileName); NodeList nodes = do

我有一个简单的.xml文件,需要对其进行解析。文件如下所示:

<table name="agents">
   <row name="agent" password="pass" login="agent" ext_uid="133"/>
</table>
#text
null row

 #text
接下来,我将尝试打印值:

document = getDocument(fileName);
            NodeList nodes = document.getChildNodes();
            for (int i=0; i<nodes.getLength(); i++){
                Node node = nodes.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE){
                    NodeList listofNodes = node.getChildNodes();
                    for(int j=0; j<listofNodes.getLength(); j++){
                        if(node.getNodeType() == Node.ELEMENT_NODE){
                            Node childNode = listofNodes.item(j);
                            System.out.println(childNode.getNodeValue()+" " + childNode.getNodeName());
                        }
                        }
                    }
                }
在第一种和第三种情况下,NodeValue为空,在第二种情况下为null,这意味着,我猜根本没有NodeValue。
所以我的问题是如何获取name、password、login、ext\u uid的值?

childNode.getNodeValue显然是空的,因为它是一个空标记。您必须查找属性

   Node childNode = listofNodes.item(j);

   Element e = (Element)childNode;
   String name = e.getAttribute("name");
   String password= e.getAttribute("password");
   String login= e.getAttribute("login");
   String ext_uid= e.getAttribute("ext_uid");

childNode.getNodeValue显然为null,因为它是一个空标记。您必须查找属性

   Node childNode = listofNodes.item(j);

   Element e = (Element)childNode;
   String name = e.getAttribute("name");
   String password= e.getAttribute("password");
   String login= e.getAttribute("login");
   String ext_uid= e.getAttribute("ext_uid");
元素没有值,它只有属性。如果它有一个值,它看起来更像是从getNodeValue返回的值

获取数据的一种方法是迭代XML节点属性,例如:

NamedNodeMap attrs = childNode.getAttributes();

if (attrs != null) {
    for (int k = 0; k < attrs.getLength(); k++) {
        System.out.println("Attribute: "
                + attrs.item(k).getNodeName() + " = "
                + attrs.item(k).getNodeValue());
    }
}
代码的输出显示文本是由于示例XML文件中的回车\n字符,根据规范,应该保留这些字符。示例输出中的null是value-less元素的空节点值。

该元素没有值,只有属性。如果它有一个值,它看起来更像是从getNodeValue返回的值

获取数据的一种方法是迭代XML节点属性,例如:

NamedNodeMap attrs = childNode.getAttributes();

if (attrs != null) {
    for (int k = 0; k < attrs.getLength(); k++) {
        System.out.println("Attribute: "
                + attrs.item(k).getNodeName() + " = "
                + attrs.item(k).getNodeValue());
    }
}
代码的输出显示文本是由于示例XML文件中的回车\n字符,根据规范,应该保留这些字符。示例输出中的null是value-less元素中的空节点值。

请改用XPath:

    XPath xp = XPathFactory.newInstance().newXPath();

    System.out.println(xp.evaluate("/table/row/@name", doc));
    System.out.println(xp.evaluate("/table/row/@password", doc));
    System.out.println(xp.evaluate("/table/row/@login", doc));
    System.out.println(xp.evaluate("/table/row/@ext_uid", doc));
改用XPath:

    XPath xp = XPathFactory.newInstance().newXPath();

    System.out.println(xp.evaluate("/table/row/@name", doc));
    System.out.println(xp.evaluate("/table/row/@password", doc));
    System.out.println(xp.evaluate("/table/row/@login", doc));
    System.out.println(xp.evaluate("/table/row/@ext_uid", doc));