Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6599713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:31:36+00:00 2026-05-25T18:31:36+00:00

Hi I need to copy nodes from one XML into another one: String _sPathOfrObj

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:31:37+00:00Added an answer on May 25, 2026 at 6:31 pm

    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:

    private static final String CLASS_NAME = "SendQueryTask";
    //...
    import java.io.StringWriter;
    
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.TransformerFactoryConfigurationError;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    //...
    private String transformXmlToString(Node node)
    {
        Transformer transformer = null;
        try
        {
            transformer = TransformerFactory.newInstance().newTransformer();
        }
        catch (TransformerConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (TransformerFactoryConfigurationError e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if ( transformer != null )
        {
            transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
    
            // initialize StreamResult with File object to save to file
            StreamResult result = new StreamResult( new StringWriter() );
            DOMSource source = new DOMSource( node );
    
            try
            {
                transformer.transform( source, result );
            }
            catch (TransformerException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            String xmlString = result.getWriter().toString();
            Log.i( CLASS_NAME, "flattened=" + (xmlString) );
    
            return xmlString;
        }       
        return null;
    }
    

    and to convert it back to Node

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import org.apache.http.protocol.HTTP;
    import java.io.UnsupportedEncodingException;
    
    private Node dumbNodeCopy(Node item)
    {
        String xmlString = transformXmlToString( item );
    
        if ( xmlString != null )
        {
            InputStream is = null;
            try
            {
                is = new ByteArrayInputStream( xmlString.getBytes( HTTP.UTF_8 ) );
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
    
            if ( is != null )
            {
                Document doc = null;
                dbf = DocumentBuilderFactory.newInstance();
                try
                {
                    db = dbf.newDocumentBuilder();
                    doc = db.parse( is );
                    doc.getDocumentElement().normalize();
    
                }
                catch (ParserConfigurationException e)
                {
                    e.printStackTrace();
                }
                catch (SAXException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
    
                if ( doc != null ) return doc.getFirstChild();
            }
            return null;
        }
        return null;
    

    and finally the copying

    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
        {
            Node copy = dumbNodeCopy( nodes4.item( i ) );
            if ( copy != null )
            {
                // nodes3.item( i ).appendChild( copy );
                nodes3.item( i ).appendChild(
                        nodes3.item( i ).getOwnerDocument().adoptNode( copy ) );
            }
            else
                Log.e( CLASS_NAME, "Copy of nodes4#" + i + ", failed." );
        }
        catch (DOMException dome)
        {
            dome.printStackTrace();
            Log.e( CLASS_NAME, dome.code + "" );
        }
    }
    

    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:)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to copy about 40 databases from one server to another. The new
I need to copy several tables from one DB to another in SQL Server
I need to copy a node and its sub-nodes: first: one identical copy followed
I need to copy a text from a textbox into the clipboard with ASP.NET.
I need to copy files from recent build folder to another folder used for
I must copy a <script> node from xml to html, but I need to
Is there any way to copy (deep) element from one DOMDocument instance to another?
I need to copy some records from our SQLServer 2005 test server to our
I need to copy a set of DLL and PDB files from a set
I need to copy an entire database from a SQL Server 2005 on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.