Issue with XML child nodes iteration when mix of text and element nodes Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Issue with XML child nodes iteration when mix of text and element nodes without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I was trying to parse following strings to form a xml document and then trying to extract all child nodes of and add to a different document object which is already available to me.

<dhruba><test>this</test>that<test2>wang chu</test2> something.... </dhruba>

<dhruba>this is text node <test>this</test>that<test2>wang chu</test2> anything..</dhruba>

while I am trying to read the child nodes, it is returning null child for TEXT_NODE for 1st string and null for ELEMENT_NODE for 2nd String, this is wrong, is it API problem ??

I am using following code … it compile , I am using java 6.

        Node n = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                try {
                    db = dbf.newDocumentBuilder();
                } catch (ParserConfigurationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                dom = db.newDocument();
                Element rootEle = dom.createElement("resources");
        // adding the root element to the document
        dom.appendChild(rootEle);

        Element element = dom.createElement("string");

        element.setAttribute("name", "some_name");
        try {

            n = db.parse(new InputSource(new StringReader("<dhruba><test>this</test>that<test2>node value</test2> some text</dhruba>"))).getDocumentElement();
            n = dom.importNode(n, true);


            NodeList nodeList = n.getChildNodes();
            int length = nodeList.getLength();
            System.out.println("Total no of childs : "+length);
            for(int count = 0 ; count < length ; count++ ){
                Node node = nodeList.item(count);
                if(node != null ){
                    element.appendChild(node);
                }
            }
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        rootEle.appendChild(element);

INPUT :: as string

             <dhruba><string name="some_name">
                        that
                        <test>this</test>                             
                        <test2>node   value</test2>
                        some text
                     </string>
              </dhruba>

EXPECTED OUTPUT :: as document

               <string>
                 <string name="some_name">
                            <test>this</test>
                             <test2>node   value</test2>
                 </string>
              </string>

if I try to parse

          <test>this</test>that<test2>wang chu</test2> something.... 

then output comes as “thiswang chu”

Why is this happening?  what needs to be done if I want to add following node under another document element, i.e. <string>.
    <test>this</test>
                        that                             
                        <test2>node   value</test2>
                        some text 
[notice that it does not have <dhruba>] inside parent node of another 
document.

Hope I am clear. Above code compiles in Java 6

Answer

Probably you want Node.cloneNode() method:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document dom = db.newDocument();

Element element = dom.createElement("string");
element.setAttribute("name", "some_name");

String inputXMLString = 
    "<dhruba><test>this</test>that<test2>node value</test2> some text</dhruba>";
Node n = db.parse(new InputSource(new StringReader(inputXMLString))).getDocumentElement();
n = dom.importNode(n, true);

NodeList nodeList = n.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i)
{
    Node node = nodeList.item(i);
    element.appendChild(node.cloneNode(true));
}
dom.appendChild(element);

To get dom into stdout or file you could write:

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource source = new DOMSource(dom);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result); 

Result:

<string name="some_name">
<test>this</test>that<test2>node value</test2> some text</string>
We are here to answer your question about Issue with XML child nodes iteration when mix of text and element nodes - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji