我的xml看起来像这样:
<item> <content><p>hello</p></content> </item>
我想通过java从内容标记中获取完整内容。但是可用的函数是getTextContent()。只返回你好。我怎样才能用标签打招呼
<p>hello</p>
使用以下Java代码:
File fXmlFile = new File("./Index.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println(eElement.getElementsByTagName("content").item(0).getTextContent());
}
}