Getting child value as string

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.

+4
source share
2 answers

Since getTextContent does not return any markup, I think this is not possible using any of the Node methods.

The only way I see (if you want to use DocumentBuilder) is that you write code to rebuild a line from nodelers (iterate over nodes and Node-attributes).

As a small sketch of what I mean: (only javalike pseudocode)

 string rebuild(NodeList nodeList) { string result = ""; for (Node n : nodeList) { result += "<" + node.getNodeName() + " "; NamedNodeMap aMap = node.getAttributes(); if (aMap != null) { int aMapLength = aMap.getLength(); for (int i=0; i<aMapLength; ++i) { Node a = aMap.item(i); result += a.getNodeName() + "=" + a.getValue() + " "; } } NodeList nList = node.getChildNodes(); if (nList == null) { result += "/>"; } else { result += ">"; result += rebuild(nList); result += "</" + node.getNodeName() + ">"; } } return result; } 

You can also create an xsd file and use xjc (JAXB) to create Java classes. There are many good tutorials on how to do this (depending on your IDE). Then you could have everything you would need to do with / without JAXB Marshal.

Another way would be to implement your own SaxHandler and use SAXParser and SAXParserFactory, which would be quite a lot of work.

+1
source

Use getTextContent () instead of the getValue () function. Below is an example (same as your getValue function).

 private static String getContent(String tag, Element element) { String value=""; try { NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes(); Node node = (Node) nodes.item(0); value=node.getTextContent(); // notice getTextContent() } catch (Exception e){ value=null; } return value; } 

It will work with well formatted xml

 <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> 
+1
source

All Articles