how to clone a DOM document?

Discussion in 'XML' started by Andy Fish, May 19, 2004.

  1. Andy Fish

    Andy Fish Guest


    I am trying to figure out how to make a complete copy of a DOM document. I
    am using xerces but vanilla DOM would obviously be the best solution. The
    nearest I have got is something like:

    Document newDoc = new DocumentImpl();


    but I presume this won't do namespace declarations, processing instructions,
    the <?xml> declaration at the front etc etc.

    I would have thought this would be a FAQ but I can't find anything obvious

    many thanks

    Andy Fish, May 19, 2004
    1. Advertisements

  2. Andy Fish

    Rolf Magnus Guest

    Try something like:

    newDoc = oldDoc.cloneNode(true);
    Rolf Magnus, May 20, 2004
    1. Advertisements

  3. Andy Fish

    Andy Fish Guest

    Hey thanks Rolf, I must admit I'm kinda surprised that worked, but it
    definitely worked for me.

    Normally calling clone on a node returns somethng owned by the same document
    as the original node, so I didn't think it would even be supported on a

    I notice that the javadoc for org.w3c.dom does not mention cloning a
    document as a special case, but the w3c core specification says cloning a
    document is implementation specific.

    Andy Fish, May 20, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.