我有类似的xml:
<parent>
<child1>
<child2>
<child3>
</parent>
现在父母标签有3个孩子。所以我需要在父标签中获得childern的数量。
我正在使用以下代码,但它无效:
NodeList nodeList = doc.getElementsByTagName("a"); nodeList.getChildnodes();
请问怎么做?
答案 0 :(得分:3)
无论伙计我感谢您的评论
NodeList image = doc.getElementsByTagName("Point");
Node singleTerminalNode = image.item(i);
Element firstLevel = (Element)singleTerminalNode;
NodeList value1Nodes = (firstLevel).getElementsByTagName("ImageFile");
int lengthofimage = value1Nodes.getLength();
答案 1 :(得分:2)
File file = new File(new File(".").getPath() + "\\src\\yourFile.xml");
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
int childrenCount = doc.getChildNodes().item(0).getChildNodes().getLength();
答案 2 :(得分:2)
Document doc = docBuilder.parse("YOUR_FILE_PATH");
Node tag1 = doc.getElementsByTagName("YOUR_NODE");
NodeList list1=tag1.getChildNodes();
System.out.println(list1.getLength());
确保导入org.w3c.dom。*和javax.xml。*