How to merge two XML's with JDOM?

How to merge two xmls's using jdom? like:
is there a way to add the complete content one xml into the parent node of another xml?
//in.xml
<?xml version="1.0"?>
<people>
<person>
  <name>ABC</name>
  <email>[email protected]</email>
</person>
</people>
//out.xml
<?xml version="1.0"?>
<address>
<city> abccounty</city>
<state> abcstate</state>
</address>
Merged XML:
<?xml version="1.0"?>
<people>
<person>
  <name>xyz</name>
  <email>[email protected]</email>
</person>
<address>
<city> abccounty</city>
<state> abcstate</state>
</address>
</people>
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class MergeXMLS {
     public static void main(String[] args) {
          try{
              SAXBuilder builder = new SAXBuilder();
              Document books = builder.build("D:/in.xml");
              Document onebook = builder.build("D:/out.xml");
              Element root = books.getRootElement();
              List rows = root.getChildren();
              for (int i = 0; i < rows.size(); i++) {
                  Element row = (Element) rows.get(i);
                  onebook.getRootElement().addContent(row.detach());
                  System.out.println(row.getName());
              new XMLOutputter(Format.getPrettyFormat()).output(onebook, System.out);
          }catch(Exception e){
               e.printStackTrace();
}

The above code only add's the first node.
I changed the code little differently to
              SAXBuilder builder = new SAXBuilder();
              Document books = builder.build("D:/in.xml");
              Document onebook = builder.build("D:/out.xml");
             //trying to add second xml into the first
              books.getRootElement().addContent(onebook.getRootElement().getContent()); 
              new XMLOutputter(Format.getPrettyFormat()).output(books, System.out);
here is the exception from the above code:
org.jdom.IllegalAddException: The Content already has an existing parent "address"
     at org.jdom.ContentList.add(ContentList.java:218)
     at org.jdom.ContentList.add(ContentList.java:140)

Similar Messages

  • How To read an XML file with JDom

    I have read through some tutorials after installing JDom on how to read an existing XML file and I was confused by all of them. I simply want to open an XML file and read one of the node's content. Such as <username>john doe</username> this way I can compare values with what the user has entered as their username. I am not sure were to start and I was hoping someone could help me out.
    I know that this seems like an insecure way to store login information but after I master opening and writing XML files with JDom I am going to use AES to encrypt the XML files.

    Here is a test program for JDom and XPath use considering your XML file is named "test.xml" :import org.jdom.input.*;
    import org.jdom.xpath.*;
    public class JDomXPath {
    public static void main(String[] args) {
      SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
      try {
       org.jdom.Document jdomDocument = saxBuilder.build("test.xml");
       org.jdom.Element usernameNode = (org.jdom.Element)XPath.selectSingleNode(jdomDocument, "//username");
       System.out.print(usernameNode.getText());
      } catch (Exception e) {
       e.printStackTrace();
    }(tested with Eclipse)

  • How to merge two xmls.

    Hi All,
    I want to merge two xmls in my output. My scenario is I am getting two sets of outputs and I want to merge them to get my final output. I my output I have a header and a Lineitem. I my final output I want to keep header only once and all the LineItems one by one.Can anyone suggest something.
    First outout:-
    <outputVariable>
    -<part name="payload" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    -<SiebelOrderQueryByExample_Output xmlns="http://siebel.com/asi/">
    -<ns:SiebelOrderQueryByExample_Output xmlns:ns="http://siebel.com/asi/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    -<ListOfOrderInterface xmlns="http://www.siebel.com/xml/Siebel%20Order">
    <Orders>
    <Id>1-15IXJ</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <ListOfLineItems>
    <LineItems>
    <Id>1-15IY3</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <LineNumber>301</LineNumber>
    </LineItems>
    </ListOfLineItems>
    </Orders>
    </ListOfOrderInterface>
    Second output:-
    <outputVariable>
    -<part name="payload" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    -<SiebelOrderQueryByExample_Output xmlns="http://siebel.com/asi/">
    -<ns:SiebelOrderQueryByExample_Output xmlns:ns="http://siebel.com/asi/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    -<ListOfOrderInterface xmlns="http://www.siebel.com/xml/Siebel%20Order">
    -<Orders>
    <Id>1-15IXJ</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <ListOfLineItems>
    <LineItems>
    <Id>1-15IW2</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <LineNumber>302</LineNumber>
    </LineItems>
    </ListOfLineItems>
    </Orders>
    </ListOfOrderInterface>
    The required output which I want after merging.
    <outputVariable>
    -<part name="payload" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    -<SiebelOrderQueryByExample_Output xmlns="http://siebel.com/asi/">
    -<ns:SiebelOrderQueryByExample_Output xmlns:ns="http://siebel.com/asi/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    -<ListOfOrderInterface xmlns="http://www.siebel.com/xml/Siebel%20Order">
    -<Orders>
    <Id>1-15IXJ</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <ListOfLineItems>
    <LineItems>
    <Id>1-15IW2</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <LineNumber>302</LineNumber>
    </LineItems>
    <LineItems>
    <Id>1-15IY3</Id>
    <OrderNumber>1012-1234</OrderNumber>
    <LineNumber>301</LineNumber>
    </LineItems>
    </ListOfLineItems>
    </Orders>
    </ListOfOrderInterface>
    Thanks

    I am not using file adapter. I am taking inputs from the BPEL client and displaying output to the client. My input xsd is:-
    <?xml version="1.0" encoding="UTF-8" ?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.org/ns/intorder"
    targetNamespace="http://www.example.org/ns/intorder"
    elementFormDefault="qualified">
    <xsd:element name="order" type="orderType">
    <xsd:annotation>
    <xsd:documentation>
    A sample element
    </xsd:documentation>
    </xsd:annotation>
    </xsd:element>
    <xsd:complexType name="orderType">
    <xsd:sequence>
    <xsd:element name="Order_Number" type="xsd:string"/>
    <xsd:element name="Line_item" type="LineType" maxOccurs="unbounded" minOccurs="0"/>
    </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="LineType">
    <xsd:sequence>
    <xsd:element name="Line_Number" type="xsd:string" />
    </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>

  • How to merge two java files with InputDialog to select?Please help me!?

    //Addition Java application
    import javax.swing.JOptionPane; // import the class
    public class Addition {
    // main method
    public static void main(String[] args)
    String firstNumber, secondNumber;
    int number1, number2, sum;
    // read the first number
    firstNumber = JOptionPane.showInputDialog("Please enter a integer: ");
    // read the second number
    secondNumber = JOptionPane.showInputDialog("Please enter another integer: ");
    // data type conversion
    number1 = Integer.parseInt(firstNumber);
    number2 = Integer.parseInt(secondNumber);
    sum = number1 + number2;
    // display the result
    JOptionPane.showMessageDialog(null, "The sum is " + sum + ".", "Results", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);
    //Multiplication Java Application
    import javax.swing.JOptionPane;
    public class Multiplication5
    public static void main(String args[])
    int number1, number2, number3, number4, number5, product;
    String firstNumber, secondNumber, thirdNumber, forthNumber, fifthNumber ;
    firstNumber =
    JOptionPane.showInputDialog("Please input an integer");
    secondNumber =
    JOptionPane.showInputDialog("Please input another integer");
    thirdNumber =
    JOptionPane.showInputDialog("Please input the third integer");
    forthNumber =
    JOptionPane.showInputDialog("Please input the forth integer");
    fifthNumber =
    JOptionPane.showInputDialog("Please input the fifth integer");
    number1 = Integer.parseInt(firstNumber);
    number2 = Integer.parseInt(secondNumber);
    number3 = Integer.parseInt(thirdNumber);
    number4 = Integer.parseInt(forthNumber);
    number5 = Integer.parseInt(fifthNumber);
    product = number1 * number2 * number3 * number4 * number5;
    JOptionPane.showMessageDialog(null, "The product is " + product, "Results", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);
    I seek for help to merge above two java application files.
    I need to call JoptionPane.showInputDialog to prompt the user to input the operation, 1 for addition, 2 for multiplication. In this dialog, the user is expected to enter the integer of 1 or 2 for the calculation.
    Please help me ! Thank you!

    Hi CRay,
    You just need to call the main methods of the 2 classes according to "1" or "2" entered.
    It is better if the "multiplication" and "addition" are declared as methods rather than in main methods.
    Example:-
    public static void Addition()
    Then call
    Addition.addition();
    than
    Addition.main(new String[]{});
    as shown below.
    import javax.swing.JOptionPane; // import the class
    public class Test{
        public static void main(String[] args){
            // read which  operation to perform
            String operation = JOptionPane.showInputDialog("Please enter which operation : ");
            if(operation.equals("1")){
                Addition.main(new String[]{});
            else{
                Multiplication5.main(new String[]{});
    }Rose

  • Merging two xmls

    Hi,
    Just for information..
    How to merge two xmls
    1. convert the xml documents to xml variables using setValue activity. Only equating will convert xmlDoc to xml Variable.
    2. serialize() each xml input variable. Each variable will get converted to string
    3. concatenate both serialized variable and assing it to xml output variable. deserialization is not require. after concatenationg add root nodes if require.
    Regards
    Sunil

    Thanks for the reply!
    Can you please tell me in steps how can i achieve this using JAXP and DOM
    In steps in the sense : first convert xml's to something then do something.
    Thanks in advance.

  • How to merge two search button from different criteria

    How to merge two search button from different criteria
    this image show the question
    http://s24.postimg.org/4algthuj9/1111.jpg
    two different criteria for the same view and I need to merge it in one button as when I click
    on the button search result give from two criteria parameters

    You can!t using af:query. the af:query component comes as is. If you can't do it through the properties, you probably can't do it at all.
    As in your other thread, the way to go is to program your own search form. This way you can use a layout and functionality you like.
    For this you create a form with input fields for the values you are searching for, put them on hte page in a way you like and in the end by hitting on a button (e.g. called Search) map all inputfields to parameters of a service method you defined in your application module or VO. This service method then executes a view criteria returning all matches in the default iterator (just as if you had used af:query.
    Timo 

  • How to merge DAC Analysis RPD with my existing RPD

    Hi all,
    I need to build OBIEE load completion report. So I need merge DAC Analysis RPD with my existing RPD.. Used following RPD ((http://rnm1978.wordpress.com/2009/07/23/mark-rittmans-obiee-repository-for-dac/)
    In my existing RPD ,I am using Oracle Datawarehouse (where in all my tables are present)..My database is pointing to QA Environment.. where all the tables are having OBIAPP Schema.
    But the tables in DAC are comin from OBISchema.. So how to merge the DAC Analysis RPD with my Exisitng RPD.
    Need help.. Urgent
    Edited by: 796797 on Jul 13, 2011 10:50 AM

    if you want to merge two rpds and combine them into a single RPD you can follow this links
    http://www.oracle.com/webfolder/technetwork/tutorials/obe/fmw/bi/biee/r1013/merge/merge.html
    http://varanasisaichand.blogspot.com/2010/03/how-to-merge-two-repositories.html
    http://download.oracle.com/docs/cd/E14571_01/bi.1111/e10540/mngreposfiles.htm#insertedID3
    http://gerardnico.com/wiki/dat/obiee/bi_server/obiee_repository_merge

  • How to compare two xml schemas

    Hello guys,
    How to compare two xml schemas? is there any tool for that?
    Thanksinadvance
    kavita//

    XML Files may be compared with the oracle.xml.differ.XMLDiff class.
    A file consisting of the differences in the xml files gets generated. An XSLT to convert one file to the other also gets generated.

  • How to merge two cells in a row

    hi freinds,
    how to merge two cells in alv grid using oops.
    for eg ,in the first row there is text premraj
             n in the second row there is cheguri
              my requriment is two merge the two cells ie to remove the horizontal line between them
    thanks & regards,
    premraj

    Well, as a new member you should learn to search the forum first before posting questions. Using keywords like "jtable merge cell" will find other postings on this topic. Some with solutions some without, so you will need to read a few and decide on the approach you want to take.

  • How to merge two adjacent partitions?

    How to merge two adjacent partitions?

    The formal steps are delete the second partition and right click on first one, choose "extend".
    Of course this will erase data on second partition.
    Some third party tools can help do the job with keep data on second partition. 
    If you have any feedback on our support, please send to [email protected]

  • How to merge two Outlook 2010 pst data files?

    How to merge two Outlook 2010 pst data files?

    Easy way: with the one you want to use open in Outlook and the other one not in your profile, go to File, Open, Import.
    If you only want to merge some content, open both pst files in outlook and drag between folders. For calendar, use a list view to see all of the appointments.
    Diane Poremsky [MVP - Outlook]
    Outlook Daily Tips |
    Outlook & Exchange Solutions Center
    Subscribe to Exchange Messaging Outlook weekly newsletter

  • How to merge two conflicting contact on iPhone and on laptop?

    How to merge two conflicting contacts?
    When syncing my iPhone w/ my Mac laptop, it seems that Apple does not provide an option that allows you to merge the data from two conflicting contacts.  For example, you have a contact on your iPhone for Andy Summer and a contact on your laptop for Andy Summers - one contact contains only Andy's name and email address, the other contact contains only Andy's name and his mailing address.  I'd like to be able to merge the data from these two contacts into one and save it to both, but the Conflict Resolver in the syncing program (I think it's in iTunes) does not offer an option for merging the two.
    Does anyone have any knowledge about 1) how to merge data from two conflicting contacts, or 2) some other way to keep the data from each so that I don't have to choose one over the other?

    Jawad,
    First of all,You need to make teaming on both the interfaces on the server side.Which mode do you want to use? Active-Active or Active-Standby.
    Server side :Active-Active - You need to configure the switch side to be a etherchannel/LACP. It can be done by the commands on the cisco 4006.
    Server side :Active-Standby - You need nothing to do at the switch side.
    Note: Seems PAGP is a proprietary of cisco. Server shouldn't know that. To be bound with mode "on" of "lacp" should be fine. It depends on the server side as well.
    HTH
    Thot

  • How to use Two main windows with in a page in script ?

    Hi any body explain me...
    How to use Two main windows with in a page in script ?
    with  header data in one main window,
    & Item data in other main window.

    HI..,
    u need to go for <b>SPLITTING THE MAIN WINDOW</b> !!!
    Main windows in page windows allow you to format text in multiple columns. Define an area in the page window, in which to position the main windows.
    Here is the procedure !!
    -->Create a page window and assign it to a page.
    Choose <b>Edit --> Main windows</b>.
    A dialog box appears.
    -->Enter values in the fields <b>Area width</b> and A<b>rea height</b> in accordance with the input guidelines for main windows.
    -->Enter values in the fields <b>Spacing</b> and Number in the <b>Horizontal group</b> if you want to use multiple columns. You can ignore the fields in the Vertical group.
    Determine how many columns and line areas are required for label printing. Then enter the corresponding values in the fields in the <b>Horizontal and Vertical groups</b>.
    -->The value in the field Left margin varies from main window to main window if multiple columns are used. The following applies:
    <b>
    Left margin of current column + Window width + Horizontal spacing = Left margin of next column</b>
    In label printing, the field Upper margin also varies from main window to main window:
    <b>
      Upper margin of current main window +  Window height + Vertical spacing = Upper margin of next main window</b>
    -->Enter a value in the field Start position.
    This is a counter. Enter a starting value which is equal to or greater than 1.
    -->The main windows are added to the list.
    -->Save your form.
    reward if it helps u...
    sai ramesh

  • How to merge two repositories using(MUD)

    hi all,
    how to merge two repositories using mude, for example i have sh rpd and paint rpd how to merge two repositories using Multi user development(mud), please don't give venkat forums link and oracle's training doc links.
    Is this possible? if it is possible please help me .
    Thanks
    Naresh

    Hi Naresh,
    What you could do is:
    1. do a ontime command line merge of both repository using the nqudmlexec tool
    2. In the mergerd repository created two project (sh and paint) and assign one bussiness model to each project.
    3. Make this the master repository for your MUD.
    4. Check the projects in and out.
    regards
    John
    http://obiee101.blogspot.com

  • Merge two XML files From java

    hi,
    i'm in need to merge two XML files from my java code please help me as soon......

    Rajesh42 wrote:
    hi,
    i'm in need to merge two XML files from my java code please help me as soon......Hello Rajesh42,
    While it is wonderful that you have discovered the magically code tags, please consider if it is the right time to use them. Code tags should only go round code. Note used to make your posts "stand out", or used to quote other people. Just code.
    As to your question, what have you tried, what are you stuck on?
    Mike

Maybe you are looking for

  • Payment Instruction Output

    Hello Guys, My client want their payment instructions output in USD not in local currency. Payment Instruction is an output we generate. When we run our payment run and take to bank to transfer money to our vendors account. Have configured F as outgo

  • How to open PO line item

    how to find  out OPEN PO line item by  using EKKO & EKPO table

  • Aperture 3.03 Weird Behavior

    All 46 images in a folder suddenly disappeared into thin air during a session editing images, I've searched everywhere in my intel imac without success. Fortunately I've been able to re-import these images from my 'Saved Original Raw File', but I'm n

  • Third party software to limit length

    I'm getting ready to help a guy, who owns a dance studio, buy a Mac and digitize his library. At his dances he likes to keep the songs under 3 minutes. Is there an easy to use program that works with iTunes that would stop playback after a set period

  • Enable TRIM on SSD Mac OS X Mountain Lion

    I recently purchased and installed a Samsung SSD 840 Pro series on my MacBook Pro. I was reading up on SSDs and I heard something about enabling TRIM and how it makes the SSD last longer. How can I enable TRIM on my SSD on my MacBook Pro that's runni