The best way to explain yourself is to show you a piece of code:
This is my XML file that I am processing:
<module> <name>name1</name> <type>type</type> <content> <p>This is some piece of code that should be treated as a full string, even that 'p' tag, because I want to use all content inside p tag for a webview in android. </p> <h1>This is a big classy title in html</h1> </content> </module>
As you can read in the p tag, basically I want to get the contents of the <content> and save it in String for processing. So at the end, I want the string to be initialized as follows:
String content = "<p> This is some piece.......</p> <h1>This is....</h1>";
This is my code that I use to get the <name> , <type> values:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(contingut); doc.getDocumentElement().normalize(); NodeList nodes = doc.getElementsByTagName("module"); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Element element = (Element) node; if(element.getNodeType() == Element.ELEMENT_NODE){ System.out.println(getValue("name",element)); System.out.println(getContent("content",element)); } } private static String getValue(String tag, Element element) { String value=""; try { NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes(); Node node = (Node) nodes.item(0); value=node.getNodeValue(); } catch (Exception e){ value=null; } return value; }
So, for example, during parsing, the name is printed correctly name1 , but the content returned empty.
Any idea how I can get the contents of <content> as a string?
Thanks.
EDIT
private static String getContent(String tag, Element element) { String value=""; try { Node nodes = element.getElementsByTagName(tag).item(0); value = nodes.getTextContent(); } catch (Exception e){ value=null; } return value; } Log.d("debugging",getContent("content",element));
And it prints this: %20%20%20%20%20This%20some%20piece ....
It does not seem to return a <p> string.