Move xml nodes from one document to another

Discussion in 'Java' started by kapiladhanvi@gmail.com, Jan 22, 2009.

  1. Guest

    Hi Everyone,

    I need to move nodes in a XML document ( doc1 ) in namespace A to
    another XML document (doc2) in namespace B.

    I have tried adoptNode() as shown below but the nodes in doc2 still
    have namespace URI for the nodes as namespace A. Below is the code
    ( this is just to provide a gist of what I am doing, not exactly
    compilable code here ) -

    function Document swapNodesAndReturnNewDoc( String namespaceB,
    Document doc1 )
    {
    DocumentBuilderFactory dbf =
    DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc2 = db.newDocument();
    doc2.setDocumentURI( namespaceB );

    Element root = doc1.getDocumentElement();

    Node clonedNode = root.cloneNode(true);
    doc2.adoptNode( clonedNode );
    doc2.appendChild( clonedNode );

    return doc2;
    }

    I have tried searching google and couldn't help around this. I have
    tried importNode() too earlier and had the same results.. I would be
    really glad to get any pointers as to where I might be going wrong.

    Thanks
    Dhanvi
     
    , Jan 22, 2009
    #1
    1. Advertising

  2. wrote:
    > Hi Everyone,
    >
    > I need to move nodes in a XML document ( doc1 ) in namespace A to
    > another XML document (doc2) in namespace B.
    >
    > I have tried adoptNode() as shown below but the nodes in doc2 still
    > have namespace URI for the nodes as namespace A.


    Yes. Neither importNode() nor adoptNode() will change the fully
    qualified name of the node. To change it, you'll have to create new
    instances of Element. Note that Document.setDocumentURI() sets the
    location at which the document is found, not any element's namespace.
     
    Mike Schilling, Jan 22, 2009
    #2
    1. Advertising

  3. Arne Vajhøj Guest

    wrote:
    > I need to move nodes in a XML document ( doc1 ) in namespace A to
    > another XML document (doc2) in namespace B.
    >
    > I have tried adoptNode() as shown below but the nodes in doc2 still
    > have namespace URI for the nodes as namespace A. Below is the code
    > ( this is just to provide a gist of what I am doing, not exactly
    > compilable code here ) -


    > I have tried searching google and couldn't help around this. I have
    > tried importNode() too earlier and had the same results.. I would be
    > really glad to get any pointers as to where I might be going wrong.


    A different namespace means a completely different node.

    I think you will need to manually create a new element
    from the old element.

    And if there are sub element you will need to do it
    recursively.

    Arne
     
    Arne Vajhøj, Jan 23, 2009
    #3
    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. gavnosis
    Replies:
    0
    Views:
    534
    gavnosis
    Aug 2, 2003
  2. loveNUNO
    Replies:
    2
    Views:
    949
    loveNUNO
    Nov 20, 2003
  3. Tony Prichard
    Replies:
    0
    Views:
    760
    Tony Prichard
    Dec 12, 2003
  4. Chris
    Replies:
    5
    Views:
    653
    David Carlisle
    Mar 17, 2005
  5. th3dude
    Replies:
    0
    Views:
    439
    th3dude
    Jan 3, 2007
Loading...

Share This Page