【发布时间】:2015-06-23 07:09:57
【问题描述】:
我有一个简单的.xml 文件,需要对其进行解析。文件如下:
<table name="agents">
<row name="agent" password="pass" login="agent" ext_uid="133"/>
</table>
我需要获取name, password, login, ext_uid 的值来创建数据库记录。
我为此做了什么:
创建了一个or.w3c.dom.Document:
public Document getDocument(String fileName){
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setValidating(false);
DocumentBuilder builder = f.newDocumentBuilder();
return builder.parse(new File(fileName));
}
接下来我正在尝试打印值:
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());
}
}
}
}
我使用它是因为我试图找出如何获取值:childNode.getNodeValue()+" " + childNode.getNodeName()
但结果如下:
#text
null row
#text
在第一种和第三种情况下,NodeValue 为空,而在第二种情况下,它为空,这意味着,我猜根本没有 NodeValue。
所以我的问题是如何获取name, password, login, ext_uid 的值?
【问题讨论】: