我有一個xml嘗試解析&閱讀它,但不知道xml可能包含多少個節點?所以我試圖讀取節點&節點的值?如何在java中獲取來自XML的節點和節點值
我如何得到相同的說:
<company>
<personNam>John</personName>
<emailId>[email protected]</emaiId>
<department>Products</department>
(may have additionaly nodes & values for same)
</company>
對不起忘了加我的代碼,使用DOM: -
Document document = getDocumentBuilder().parse(new ByteArrayInputStream(myXML.getBytes("UTF-8")));
String xPathExp = "//company";
XPath xPath = getXPath();
NodeList nodeList = (NodeList)xPath.evaluate(xPathExp, document, XPathConstants.NODESET);
nodeListSize = nodeList.getLength();
System.out.println("#####nodeListSize"+nodeListSize);
for(int i=0;i<nodeListSize;i++){
element=(Element)nodeList.item(i);
m1XMLOutputResponse=element.getTextContent();
System.out.println("#####"+element.getTagName()+" "+element.getTextContent());
}
是否使用SAX解析器或DOM解析器的XML?你能發表你寫的代碼嗎? – 2011-04-10 09:11:20
你正在使用什麼語法分析器,你想用數據做什麼?你能發佈迄今爲止的代碼嗎? – 2011-04-10 09:11:27
Dupe的http://stackoverflow.com/questions/4076910/how-to-retrieve-element-value-of-xml-using-java – 2013-08-01 15:54:34