Xml 如何在Java中使用DOM获取子节点
我有这个XMLXml 如何在Java中使用DOM获取子节点,xml,dom,Xml,Dom,我有这个XML <employees> <employee tag="FT" name="a"> <password tag="1"/> <password tag="2"/> </employee> <employee tag="PT" name="b"> <password tag="3"/> <password tag="4"/> </empl
<employees>
<employee tag="FT" name="a">
<password tag="1"/>
<password tag="2"/>
</employee>
<employee tag="PT" name="b">
<password tag="3"/>
<password tag="4"/>
</employee>
</employees>
我试图获取每个员工的子节点,并将子节点的标记值放入。
列表中密码的标记值
nl = doc.getElementsByTagName("employee");
for(int i=0;i<nl.getLength();i++){
NamedNodeMap nnm = nl.item(i).getAttributes();
NodeList children = nl.item(i).getChildNodes();
passwordList = new ArrayList<String>();
for(int j=0; j<children.getLength();j++){
NamedNodeMap n = children.item(j).getAttributes();
passwordTagAttr=(Attr) n.getNamedItem("tag");
passwordTag=stopTagAttr.getValue();
passwordList.add(passwordTag);
}
}
nl=doc.getElementsByTagName(“员工”);
对于(int i=0;i由getChildNodes()
返回的NodeList
包含元素
子节点(在本例中,这是您关心的)以及节点
本身的属性子节点(您不关心)
<代码>为(int j=0;j不受欢迎!如果您找到一个帮助您的答案,请考虑投票或将其标记为已接受的答案。
for(int j=0; j<children.getLength();j++) {
if (children.item(j) instanceof Element == false)
continue;
NamedNodeMap n = children.item(j).getAttributes();
passwordTagAttr=(Attr) n.getNamedItem("tag");
passwordTag=stopTagAttr.getValue();
passwordList.add(passwordTag);
}