Get nested xml document in DOm java



What am i trying to do? - I am trying to extract the InputData document in below xml as XML(not as textContent of elements).



<?xml version="1.0" encoding="UTF-8"?>
<Test>
<Identifier>1</Identifier>
<Mode>SYNCHRONOUS</Mode>
<Callback>null</Callback>
<InputData>
<iid>
<test3>p</test3>
<test4>p1</test4>
</iid>
</InputData>
</Test>


When i do node.item(i).textContent(), I want output as :



<iid>
<test3>p</test3>
<test4>p1</test4>
</iid>


What is the problem Code? -



private void doProcessing(Document xmldoc) {

Element rootEliment = xmldoc.getDocumentElement();
// System.out.println("NodeName : "+rootEliment.getNodeName());
NodeList children = rootEliment.getChildNodes();
NamedNodeMap nnl = rootEliment.getAttributes();

Node inputdata = nnl.getNamedItem("InputData");
// inputdata.getOwnerDocument().g
// System.out.println("inputDataValuefirst: "+inputdata.getTextContent());
// NodeList testList = xmldoc.getElementsByTagName("InputData");
for (int i = 0; i < children.getLength(); i++) {
// Element el = (Element)children.item(i);
//System.out.println("NodeName: " + children.item(i).getNodeName()
// + " TextValue: " + children.item(i).getTextContent());
if (children.item(i).getNodeName() == "InputData") {
NodeList childs = children.item(i).getChildNodes();
for (int j = 0; j < childs.getLength(); j++) {
System.out.println("NodeVal;ue: "
+ childs.item(j).getNodeName() + " Text: "
+ childs.item(j).getTextContent());
}

}
}
}


gives me: NodeVal;ue: iid Text: pp1


I have also tried various other combinations. All of them give the textContent of child nodes but not the node itself. How can i achieve this?


No comments:

Post a Comment