0

I'm having two xml files as shown below and I need to read a node from one xml and append that node to another xml file.

Xml file :1

<A>
  <B>
    <c>1<c/>
    <d>2<d/>
    <e>3<e/>
  </B>
</A>

Xml file :2

<AA>
  <BB>
    <cc>1<cc/>
    <dd>2<dd/>
    <ee>3<ee/>
    <ff>
       <gg>4</gg>
    <ff>
  </BB>
</AA>

RESULT

<A>
    <B>
      <BB>
        <cc>1<cc/>
        <dd>2<dd/>
        <ee>3<ee/>
        <ff>
           <gg>4</gg>
        <ff>
      </BB>
        <c>1<c/>
        <d>2<d/>
        <e>3<e/>
    </B>
</A>

Any advice you could give is much appreciated.

5
  • Read both files, combine nodes in memory, write to a third file... Commented Mar 27, 2013 at 14:56
  • Hi Lucas,I have read the both files and i'm not able to fetch the node from a xml file:2.Can u tell me how to do that Commented Mar 27, 2013 at 15:09
  • Use a SAX, StAX, or DOM parser... Commented Mar 27, 2013 at 15:12
  • @Lucas I'm using DOM parser but I couldn't get those node from xml file 2.can you give me a sample code for getting those node as bunch and it will be helpful.. Commented Mar 27, 2013 at 15:37
  • Im not sure there is a way to get "node as bunch". You may have to walk the tree adding each node and child node one at a time. Commented Mar 27, 2013 at 15:44

1 Answer 1

1

To expand on Lucas's first comment above you could try using SimpleXML to read the two XML files into memory aka 'deserialization'. Example code for reading XML into in memory objects looks like this:

Serializer serializer = new Persister();
File source = new File("example.xml");

Example example = serializer.read(Example.class, source);

Then, once you have combined the two results into a third object you can also use SimpleXML to write the third object to a file aka 'serialization'.

Example code for writing an XML file using in memory data:

Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("example.xml");

serializer.write(example, result);

SimpleXML has a good tutorial and can be imported by adding a Maven dependency.

Spend a little time reading the tutorial and it should get you going in the right direction.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.