Howto: Extract Node/sub-tree of a Document and form a new XMLDocument

Discussion in 'XML' started by Casper B, Jan 25, 2005.

  1. Casper B

    Casper B Guest

    As the topic says, I am wondering how (in Java) I can extract a sub-tree
    of a org.w3c.dom.Document and form a brand new one. I use Oracle's
    XMLDocument implementation and have been playing around with
    DocumentFragment etc. but somehow I always end up creating only the
    single node and not its sub-nodes.

    /Casper
     
    Casper B, Jan 25, 2005
    #1
    1. Advertising

  2. /Casper B/:

    > As the topic says, I am wondering how (in Java) I can extract a sub-tree
    > of a org.w3c.dom.Document and form a brand new one. I use Oracle's
    > XMLDocument implementation and have been playing around with
    > DocumentFragment etc. but somehow I always end up creating only the
    > single node and not its sub-nodes.


    You probably need the 'Document.importNode' method:

    http://www.w3.org/TR/DOM-Level-2-Core/core.html#Core-Document-importNode
    http://java.sun.com/j2se/1.4.2/docs...ent.html#importNode(org.w3c.dom.Node, boolean)

    Notice the 'deep' parameter:

    > deep - If true, recursively import the subtree under the specified
    > node; if false, import only the node itself, as explained above.
    > This has no effect on Attr, EntityReference, and Notation nodes.


    Something like:

    Document doc1;
    Element elem1; // element from 'doc1'

    Document doc2;
    Element elem2; // element from 'doc2'

    // perform a deep copy of 'elem1' from 'doc1' adding
    // it as child of 'elem2' from 'doc2'
    elem2.appendChild(doc2.importNode(elem1, true));

    --
    Stanimir
     
    Stanimir Stamenkov, Jan 29, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shoval Tomer
    Replies:
    0
    Views:
    467
    Shoval Tomer
    Jul 9, 2003
  2. Bob
    Replies:
    0
    Views:
    424
  3. Carlos Albert

    Extract a <node> as a new XMLDocument?

    Carlos Albert, Jan 19, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    708
    Carlos Albert
    Jan 20, 2006
  4. Ben
    Replies:
    2
    Views:
    936
  5. Peter Mueller
    Replies:
    6
    Views:
    4,663
    Stefan Ram
    Jan 13, 2008
Loading...

Share This Page