1
<root>
<h id="1">
    <d value="1,2,3,4,5"><open>10:00</open><close>23:00</close></d>
    <d value="6"><open>10:00</open><close>2:00</close></d>
    <d value="7"><open>10:00</open><close>21:00</close></d>
</h>
<h id="2">
</h>
</root>

Here I have the XML which root has list of <h> tagged nodes. Now I need to break these into parts and set it into different variables (add into a map).

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new ByteArrayInputStream(data.getBytes("utf-8"))));
NodeList nList = doc.getElementsByTagName("h");
for (int i = 0; i < nList.getLength(); i++)
{
    Node nNode = nList.item(i);
    System.out.println(nNode.getAttributes().getNamedItem("id") + " " + ?????);        
}

what should I call in order to get the value (String value) of a nNode ?

Here is what Im looking for as the asnwer for the above code once some one fills the ????

1 <h id="1"><d value="1,2,3,4,5"><open>10:00</open><close>23:00</close></d><d value="6">open>10:00</open><close>2:00</close></d><d value="7"><open>10:00</open><close>21:00</close></d></h>
2 <h id="2"></h>

And i don't mind having as root element

dinesh707
  • 12,106
  • 22
  • 84
  • 134
  • 1
    I answered [another question](http://stackoverflow.com/a/21661452/2071828) recently about getting `String`s of XML subtrees using XPath - that is proabably the best aproach here too. – Boris the Spider Mar 14 '14 at 08:37

4 Answers4

2

You can use Node.getTextContent() to conveniently get all the text of a node (gets text of children as well).

See Parsing xml file contents without knowing xml file structure for a short example.


If you're trying to get the value attributes of the d nodes (I can't actually tell, your question is slightly unclear to me), then it would be different -- for that you would iterate through the children of each h node (use getChildNodes() or getFirstChild() + getNextSibling()) then grab their value attributes just as you are getting the id attribute of the h nodes (the above link also shows an example of iterating through child nodes).

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 1
    @dinesh707 In that case; does something from http://stackoverflow.com/questions/1219596/how-to-i-output-org-w3c-dom-element-to-string-format-in-java help? Anver's answer here is also a good option. – Jason C Mar 14 '14 at 08:33
  • And the problem with Node.getTextContent() is it just give me text. I even need the XML tags under the main node – dinesh707 Mar 14 '14 at 08:33
  • @dinesh707 Yes that is correct; it wasn't clear before your update. Check the link from my previous content and also consider jDom as described in Anver's answer. – Jason C Mar 14 '14 at 08:34
  • 1
    The comment I UP voted did the trick. thank you. But I was hoping for a simpler and direct way. Seems there is no other way. – dinesh707 Mar 14 '14 at 08:36
1

Have you tried jDom library? http://www.jdom.org/docs/apidocs/org/jdom2/output/XMLOutputter.html

 XMLOutputter outp = new XMLOutputter();
 String s = outp.outputString(your_jdom_element);
Anver
  • 116
  • 2
0

Have you tried nNode.toString() if you are using Node from javax.xml.soap.Node.

Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25
0

You can use that:

http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html#getTextContent()

but your sample nNode has other nodes, not just text. It seems you need helper method to construct String from child nodes.

Pass your nNode to nodeToString

XML Node to String in Java

Community
  • 1
  • 1
Vitalii Velikodnyi
  • 1,312
  • 11
  • 18