Hi I need to copy nodes from one XML into another one:
String _sPathOfrObj = "/response/grp/ofr/obj";
String _sPathHotelData = "/main/hotelData";
String _sPathStatus = "/main/status";
xpath.reset();
NodeList status = (NodeList) xpath.evaluate( _sPathStatus, details, XPathConstants.NODESET );
if(status.item( 0 ).getTextContent().equalsIgnoreCase( "ok" ))
{
NodeList nodes3 = (NodeList) xpath.evaluate( _sPathOfrObj, result, XPathConstants.NODESET );
NodeList nodes4 = (NodeList) xpath.evaluate( _sPathHotelData, details, XPathConstants.NODESET);
minimum = Math.min( nodes3.getLength(), nodes4.getLength() );
Log.i(CLASS_NAME, "obj="+nodes3.getLength());
Log.i(CLASS_NAME, "hdat="+nodes4.getLength());
for (i = 0; i < minimum; i++)
{
try
{
nodes3.item( i ).appendChild( nodes4.item( i ) );
}
catch(DOMException dome)
{
dome.printStackTrace();
Log.e(CLASS_NAME, dome.code+"" );
}
}
}
but it generates an error, DOMException WRONG_DOCUMENT_ERR or when I will do nodes4.item( i ).deepClone(true or false) it will throw DOMException NAMESPACE_ERR
Is there a way to do it?
Many thanks
I’ve managed to have it working, it is a mixture of my solution to overcome deepClone throwing NAMESPACE_ERR and the solution presented in the link.
Basically I wasn’t able to make a copy of the given node without an error so I am flattening the XML to string and converting the string to new Node and by using the adoptNode I have my solution:)
I do realise that it is hell of a hack but it works for my tight deadline:)
The converting to string is done by using Transformer class as follows:
and to convert it back to Node
and finally the copying
btw. don’t laugh at the code:-) I know that it looks terrible but my imperative ATM is to meet deadlines, and if anyone will propose neat solution I will happily adhere:)