How to copy a node from one dom document to another?

I have one dom document that I have to split up into multiple dom documents. I am able to get the inividual nodes that I want to put into seperate documents.
My problem occurs when I create a new dom document and try to add the node from the parent document. I get an exception saying (copied from api: WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node. )
How can I make it so that I can copy a node from one document and add it to another.

Have you checked out the API called importNode in the DOM Document. It lets you move nodes between different documents.
This api lets you simply copy the existing node from one document into another. without creating any new nodes for it.
I have done a small example please have a look.
Book.xml
<?xml version="1.0"?>
<books>
  <book>
    <name>Inside Corba</name>
  </book>
  <book>
    <name>Inside RMI</name>
  </book>
</books>------------------------
Book2.xml
<?xml version="1.0"?>
<books>
  <book>
    <name>Core Java </name>
  </book>
  <book>
    <name>Core JINI</name>
  </book>
</books>-------------------
MoveNode.java (copies nodes from doc2 into doc1)
import java.io.*;
import javax.xml.parsers.*;
// structures
import org.w3c.dom.*;
import org.xml.sax.*;
public class MoveNode
  public static void main(String[] args)
    // step1. create a factory and configure it
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // step2. set various configurations
    factory.setValidating(false); // do not need validation at this time.
    factory.setIgnoringComments(false); // do not ignore comments
    factory.setIgnoringElementContentWhitespace(false); // do not ignore element content whitespace.
    factory.setCoalescing(false);
    factory.setExpandEntityReferences(true);
    // step 3 create a document builder
    DocumentBuilder builder = null;
    try
      builder = factory.newDocumentBuilder();
    catch(ParserConfigurationException e)
      e.printStackTrace();
    try
      Document doc1 = builder.parse(new File("book.xml"));
      Document doc2 = builder.parse(new File("book2.xml"));
      if (doc1 == null || doc2 == null)
        System.out.println("doc1 is null or doc2 is null");
        System.exit(1);
      } // if
      // fetch books from doc2
      NodeList list = doc2.getElementsByTagName("book");
      System.out.println("number of books found " + list.getLength());
      Node node1 = list.item(0);
      Node node2 = list.item(1);
      // get the root node of doc1
      Node root = (Node) doc1.getDocumentElement();
      root.appendChild(doc1.importNode(node1, false));
      root.appendChild(doc1.importNode(node2, false));
      //now doc1 should have 4 nodes
      System.out.println(doc1.getElementsByTagName("book").getLength());
    } // try
    catch(SAXException se)
      se.printStackTrace();
    catch(IOException ie)
      ie.printStackTrace();
hope this helps.
regards,
Abhishek.

Similar Messages

  • How do I copy a page from one pdf  document to another?

    I am a newbe!

    That was easy.Thank you.
         Re: How do I copy a page from one pdf document to another?
    created by George Johnson in Creating, Editing & Exporting PDFs - View the full discussion
    If you open both documents, you can drag a page from the Pages panel (on the left of the window) of one document and drop it into the Pages panel of another.
    You can also extract one or more pages from a document to create a new document, and then insert the pages from this new document into another. In Acrobat 11 you 'd do this by doing:
    Tools > Pages > Extract
    and then:
    Tools > Pages > Insert from File
    Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/6163925#6163925
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/6163925#6163925
    To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/6163925#6163925. In the Actions box on the right, click the Stop Email Notifications link .
    Start a new discussion in Creating, Editing & Exporting PDFs at Adobe Community
    For more information about maintaining your forum email notifications please go to http://forums.adobe.com/thread/416458?tstart=0 .

  • How do I copy a page from one Pages document to another using Pages 5.0?

    I posted this question on another page, and haven't recieved an answer to it yet. However I have found a temporary solution, so I am posting this here in case anyone else has a similar problem.
    The issue here is that I have just upgraded from Snow Leopard to Maverick (10.9.1), and my Pages also upgraded (to 5.0.1), altering my Pages documents. This is the issue, as as far as I can see the new Pages does not offer some of the basic needs that the old Pages did! In other words it is worse than the previous version.
    Using the upgraded new Pages 5.0.1 seems to result in some major editing problems for me. If you upgrade, make sure you KEEP the old version of Pages 09 (v 4.3). That is what has saved me!  If you have the same problems I have had - explained below, you will still be able to close the new version and open your Pages document in the old version, provided it is not a new Pages document. If it is, it will be possible to export it as a Pages 09 document and edit properly.
    After upgrading to the new Pages 5.0.1, when I open documents in Pages and try and copy a section from one document to another (easy before using the thumbnails) many of the Menu items previously available are visible but appear faded and are not possible, including Insert>Section Break/Page Break/Page Number and Edit>Cut/Copy/Delete/Undo/Redo
    This even happens if I duplicate the Pages document and try and do any of these from the Duplicate (which should be an easily editable copy).
    My solution has been to simply open all Pages documents that Iwant to edit (or may want to edit in future) in the old Pages app (v 4.3), which I never got rid of. That has meant I can continue editing and copying a section (of 1 or more pages) from one Pages document to another.
    If anyone reading this has figured out a way to edit Pages documents using the new version 5.0.1 please post an explanation, and I will try it!

    And there are those who don't like me repeating the same message over and over again in this forum!
    Apple has removed over 90 features from Pages 5.
    http://www.freeforum101.com/iworktipsntrick/viewforum.php?f=22&sid=3527487677f0c 6fa05b6297cd00f8eb9&mforum=iworktipsntrick
    Pages '09 should still be in your Applications/iWork folder.
    Archive/trash Pages 5, after Exporting your files to Pages '09, and rate/review it in the App Store, then get back to work.
    Peter

  • How to copy a table from one text frame to another...

    Is there a way to copy a table from one text frame to another? I'm using JavaScript. The following will move a table from one text frame to another, but I need to copy.
    var srcFrame = document.textFrames.item("section-template");
    var dstFrame = document.textFrames.item("test");
    srcFrame.characters[0].move(LocationOptions.before, dstFrame.insertionPoints[0]);
    Thanks,
    Mike-

    Hi Bhupinder,
    According to your description, you want to copy a table with Primary keys from one database to another database.
    As per my understanding, I think the best method is use Transfer SQL Server Objects Task in SQL Server Integration Services. The Transfer SQL Server Objects task transfers one or more types of objects in a SQL Server database between instances of SQL Server.
    Server roles, roles, and users from the specified database can be copied, as well as the permissions for the transferred objects. Indexes, Triggers, Full-text indexes, Primary keys, Foreign keys can also be copied.
    To use the Transfer SQL Server Objects Task, we should create a SQL Server Integration Services Project in SQL Server Data Tools, then drag a Transfer SQL Server Objects Task to Control Flow pane. Specify SourceConnection, SourceDatabase, DestinationConnection
    and DestinationDatabase for the Connection, select the table in the ObjectsToCopy category, then change CopyPrimaryKeys to True and the other corresponding properties in the task.
    References:
    Transfer SQL Server Objects Task
    Transfer SQL Server Objects Task in SSIS 2008 R2 With Example
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • How to copy a tab from one tab group to another?

    In Tab groups view, drag and drop allows one to move one tab from one group to another. But how to copy a tab?

    Press '''CTRL''' while dragging to copy instead of moving.

  • How to copy function group from one SAP system to another

    dear all,
    our company will set up a sub-company currently,and the sub-company want to copy some programs from our SAP system.
    how to pack and copy function group?
    pls help me,tks!

    Hello  Snow zeng,
    Will the 2 systems be connected ? I.e Same land scape ?
    If they are non connected systems, check this Wiki by Marcin [How to copy Repository Objects between non-connected SAP systems|http://wiki.sdn.sap.com/wiki/display/ABAP/HowtocopyRepositoryObjectsbetweennon-connectedSAPsystems]
    Regards

  • How to Copy customizing data from one company code to another company code

    Hi
    Big Boss opened a new company, we need to implement SAP system for this new company. this new company has same business with our one old company. so, we may almost use old the company customizing data for this new company. anybody can tell me how to copy the customizing data.
    Regads
    Henry

    Hi, Paraschand G
    Thank your very good solution as BC Sets. I had general knowledge on Rollout and BC Sets now.
    How ever, I have a problem on creating my first BC Sets. Should I creat a BC Sets as my template via copy existing customizing data? I seen some existing BC Sets which arenot I want. We implemented big5 modules for some company codes. I want to copy one of them as my BC Sets template. I won't create it manually because I worried about I will miss some data if I select these data one by one. I cannot find out a Full-BC Sets template contains all customizing data. Could you please give me some guide. I cannot ensure my understanding of BC Sets. Please correct it if I have wrong idea.
    Regards
    Henry

  • How to copy an ORDImage from one DB instance to another instance

    Hello,
    I need to copy a table with ORDImage columns from one DB to a different DB in a global/distributed transaction. What is the best way to to this:
    Do I really need to do the "insert empty ORDImage, select for update, Load BLOB data into ORDImage Proxy, update" cycle for inserting the ORDImage in the target DB or is there a better way?
    Pete

    Hello,
    i want to copy my application in to another system…Are you talking about deploying a fully developed application (like in a client site) or copy your application into a different development instance?
    Regards,
    Arie.

  • Why do objects get bigger when I copy and paste from one Keynote document to another?

    I have 2 keynote presentation files open. I select some objects on one slide, apple-C to copy, go over to a blank slide in another file, and apple-V to paste. The item(s) get pasted but are much bigger - they take up way more of the new slide than they did for the old one.  What am I missing? Why can't I copy it with the same relative size it used to have?

    Chances are that small file used to be a PowerPoint file - PowerPoint deals in inches instead of pixels (which blows my mind).
    I would first convert the smaller file to a larger resolution - that should keep most of the scaling intact - and then copy and paste. In my opinion you'll probably have to re-polish your presentation once you've changed the resolution as some things will probably be a bit wonky.

  • Copying a page from one InDesign document to another

    I have a page in an InDesign document (which contains graphics) that I would like to copy to another InDesign document, but Copy / Past does not work.

    Have both files open...
    In the source file open the pages panel and select the page(s) you want to copy, from the panel menu choose move page(s), and select the destination file and specify a position.

  • Copying a page from one picture book into another

    Does anyone know how to copy a page from one picture book into another. I have made 2 books and would like to make a third consisting of pages from the first 2 but seem unable to do it without starting again, or at least starting from a duplicate of one and re doing all the missing pages.

    Garageclan
    Welcome to the Apple user to user assistance forums
    print each page you want to include in "book 3" to a PDF and use the send PDF to iPhoto option - this will give you the pages as images in iPhoto that you can them place into "book 3"
    See Old Toad's tutorial #19 for more details - http://web.mac.com/toad.hall/OldToadsTutorials/No._19.html - note that it is slightly different - you are only using the beginning part
    LN

  • How to copy OVD configuration from one machine to another?

    We have two machines with OVD servers on them. The configurations should be identical from one machine to the other. We suspect there is a problem with the configuration on one of them. We need to know how to copy OVD configuration from one machine to another.
    Can you tell us how to do that?

    well i have this in mind which may help you.
    You would need to have a public ip address to the machine you have consoled to and on internet.
    Download the tftp software from below link.
    http://tftpd32.jounin.net/
    This software does not only act as the tftp server but also you can select the interface of you ethernet card as tftp server ip address.
    For ex if you are connected to a console and have a wireless card which is connected to internet also you connect you eth lan card to the eth or fast eth of the router.
    you can select which ever interface you want to act as the tftp server.
    you will need to add ip addres for you lan card and also config the router port as same if needed.

  • How to copy a folder from one document library to another document library ?

    How to copy a folder from one document library to another document library by programmtically?
    Samarendra Swain
    Team Sharepoint
    www.manuhsolutions.com

    You can use the SPFolder.CopyTo method.
    public static void CopyFolder()
    SPFolder folder = null;
    using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
    using (SPWeb web = site.OpenWeb())
    folder = web.GetFolder("shared%20documents/newfolder");
    folder.CopyTo("tester4/newfolder");
    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.copyto.aspx
    certdev.com

  • How to copy List item from one list to another using SPD workflow using HTTP call web service

    Hi,
    How to copy List item from one list to another using SPD workflow using HTTP call web service.
    Both the Lists are in different Web applications.
    Regards, Shreyas R S

    Hi Shreyas,
    From your post, it seems that you are using SharePoint 2013 workflow platform in SPD.
    If that is the case, we can use Call HTTP web service action to get the item data, but we cannot use Call HTTP web service to create a new item in the list in another web application with these data.
    As my test, we would get Unauthorized error when using Call HTTP web service action to create a new item in a list in another web application.
    So I recommend to achieve this goal programmatically.
    More references:
    https://msdn.microsoft.com/en-us/library/office/jj164022.aspx
    https://msdn.microsoft.com/en-us/library/office/dn292552.aspx?f=255&MSPPError=-2147217396
    Thanks,
    Victoria
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • HOW TO COPY A FORM FROM ONE TO ANOTHER CLIENT

    HOW TO COPY A FORM FROM ONE TO ANOTHER CLIENT

    Hi Tina,
    To copy either a Script or a Smartform fron one client to another client i.e from reference client 000 to any client say 010  follow instructions as given below:
    Go to Tcode SE71->Give Form name MEDRUCK then go to Menu path Utilities->Copy From Client, give
    Form Name: MEDRUCK
    SOURCE:000 (it will be already there)
    Target Form: Zmedruck(here give ur form zname)
    Execute
    It will be copied into all languages.
    Then come back to SE71
    Give your form name Zmedruck
    Language:: de then goto change mode
    then menu path->utilities->convert original languge to En and enter you will get a message original language of form zmedruck converted from de to en,
    now  change language de to en in se71 main screen and then do what ever changes you want to do , this is how you can copy a script or smartform from one client to another client.
    If this answer is useful reward points any queries revert me back.

Maybe you are looking for

  • Using my MacBook as a wireless hub?

    I've had my MacBook for a couple of months now and I love it. I recently got a next-gen video game console with internet capabilities. The problem is that my router is all the way on the other side of my apartment. I could buy a 100' ethernet cord, b

  • How to use windows

    How do i use windows on my intel macbook? I heard there are two options-you can reboot your computer or you can use windows simultaneously with mac applications. which option is better? what are the pros and cons of both? which one takes up more memo

  • Maintenance order - Copy header long text in first operation.

    Hi gurus, I have the following requirement I dont know hoy to solve it. In our system, there is a certain maintenance order class for corrective operations. During creation, the short text in order header is transferred automatically to the first ope

  • Deploying Application freezing at 9%....Very Urgent

    Hi Gurus, We have installed Oracle EPM 11.1.1.1 on Windows 2003(vmware) and configured HFM. We have created profile, When we were trying to deploy one sample application from 9.3 system, progress was freezing at 9%. Its showing Details as below in Su

  • Javac compiler Error - cannot resolve symbol - symbol  StringBuilder?

    Hi , I am using hp - ux system with java version "1.4.2.06". when i try to compile a program called CharSequenceDemo.java which is found in the java tutorials at this link [CharSequenceDemo.java|http://java.sun.com/docs/books/tutorial/java/IandI/exam