0

My xml look like this:

 <item> <content><p>hello</p></content> </item>

I want to get the full content from content tag through java. but the function available is getTextContent(). which is returning only hello. How can i get hello with tags

<p>hello</p>

using Java code as below:

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());
}
}
Rank Bazaar
  • 101
  • 2
  • 6
  • Please provide more information about what dependencies are you using, and what is the object on which you call `getTextContent()`. That would be really helpful :) – SocketByte Apr 22 '18 at 21:01
  • Possible duplicate of [Which is the best library for XML parsing in java](https://stackoverflow.com/questions/5059224/which-is-the-best-library-for-xml-parsing-in-java) – Dhruv Sehgal Apr 22 '18 at 21:07
  • [Check this out](http://www.java2s.com/Tutorials/Java/XML_HTML_How_to/DOM/Convert_XML_Node_to_String.htm). Unfortunately it doesn't seems to have an easy way. – Lucas Noetzold Apr 23 '18 at 03:28
  • Thanks @LucasNoetzold for the help. Solution Working fine. – Rank Bazaar Apr 28 '18 at 20:37

0 Answers0