Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how do i merge the child elements in different XML files to a single XML

549379Dec 19 2006 — edited Aug 22 2008
Hi,
I have an immediate requirement like below:
The following input XML files which are located in source dir(example:c:\source)
input1.xml
<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employee.xsd">
<emp id="100">
<property name="ARTIST">LATA MANGESHKAR</property>
<name>raj</name>
</emp>
<emp id="101">
<property name="ARTIST">KAMAL</property>
<name>kamaj</name>
</emp>
<emp id="102">
<property name="ARTIST">PANDI</property>
<name>kuma</name>
</emp>
<name ID="10"/>
</employee>
input2.xml
<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employee.xsd">
<emp id="200">
<property name="ARTIST">Raja</property>
<name>rajes</name>
</emp>
<emp id="201">
<property name="ARTIST">Guru</property>
<name>kaajal</name>
</emp>
<emp id="202">
<property name="ARTIST">Mani</property>
<name>selvan</name>
</emp>
<name ID="10"/>
</employee>
My output.xml should look like below:
<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employee.xsd">
<emp id="100">
<property name="ARTIST">LATA MANGESHKAR</property>
<name>raj</name>
</emp>
<emp id="101">
<property name="ARTIST">KAMAL</property>
<name>kamaj</name>
</emp>
<emp id="102">
<property name="ARTIST">PANDI</property>
<name>kuma</name>
</emp>
<emp id="200">
<property name="ARTIST">Raja</property>
<name>rajes</name>
</emp>
<emp id="201">
<property name="ARTIST">Guru</property>
<name>kaajal</name>
</emp>
<emp id="202">
<property name="ARTIST">Mani</property>
<name>selvan</name>
</emp>
</employee>
The above output.xml is generated based on the child element <NAME> of <employee> root element.
i will have to import only the package import org.jdom.*;

using jdom APIs like Element, Attribute , setAttribute, etc.. i will have to build the output.xml file.
......
Document xmlDoc = new Document(employee);
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
.....


Could you people post the sample code to generate the output.xml file which contains the merging of <emp> elements based on the unique id of <name ID="10"/>

Waiting for your valuable code!!
thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 19 2008
Added on Dec 19 2006
8 comments
616 views