A tricky XML Query with Java.Please guide.
I have a tricky question here.
1).I have an XML that is generated on the fly.
XML Generated
-
Document genereatedDoc =
<Trade>
<Level>10 </Level>
<Sty> .5 </Sty>
</Trade>
2).I have to add the above XML generated in between another XML document
-
Main XML Document
-
Document messagedoc =
<Interest>
<Market>
<Size>
<Size>
</Market>
</Interest>
--
Output shud be:
--
<Interest>
<Market>
<Size>
<Trade>
<Level>10 </Level>
<Sty> .5 </Sty>
</Trade>
<Size>
</Market>
</Interest>
[code]
3)I am using Java to add the document in 1 to 2.
[code]
Java Code
Document genereatedDoc ='The generated XML document in 1.
for (int i=0; i< genereatedDoc.getLength(); i++){
try{
xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr= xpath.compile("/Interest/Market/Size");
Node result = (Node)expr.evaluate(messagedoc, XPathConstants.NODE);
NodeList nList = messagedoc.getElementsByTagName(result.getNodeName());
eL =(Element)nList.item(0);
//Make a copy of the element including any child nodes.
Element e = (Element)eL.cloneNode(true);
Element element = (Element)genereatedDoc.item(i);
Node dup =messagedoc.importNode(element,true);
//Add this before Size
messagedoc.getDocumentElement().insertBefore(eL,eL.getNextSibling()).appendChild(dup);
}catch (XPathExpressionException e){
}
}
It is unable to add the document to the child node as specified in the XPATH expression.
It adds the document only to the top level nodes like Interest,but not to the specific child node.
I want to add the generated document under element Size.
Please can somebody help me and tell me how to append the genereatedDoc after Size?

