0

I have a problem to merge two XML string or File.

XML1.

<?xml version="1.0" encoding="UTF-8"?>
<Specification>
 <item>
  <id>PS1</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P1</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P2</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR1</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR2</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
 <item>
  <id>PS2</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P3</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P4</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR3</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR4</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
</Specification>

XML2.

<?xml version="1.0" encoding="UTF-8"?>
<Specification>
 <item>
  <id>PS3</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P5</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P6</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR5</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR6</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
 <item>
  <id>PS1</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P7</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P8</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR3</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR4</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
</Specification>

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<Specification>
 <item>
  <id>PS1</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P1</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P2</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR1</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR2</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
 <item>
  <id>PS2</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P3</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P4</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR3</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR4</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
 <item>
  <id>PS3</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P5</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P6</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR5</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR6</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
 <item>
  <id>PS1</id>
  <startDate> 21 Oct 2016</startDate>
  <Product>
   <item>
    <id>P7</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
   <item>
    <id>P8</id>
    <startDate> 21 Oct 2016</startDate>
   </item>
  </Product>
  <CompositePrice>
   <item>
    <id>PR3</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>1.0</Price>
   </item>
   <item>
    <id>PR4</id>
    <startDate> 21 Oct 2016</startDate>
    <Price>2.0</Price>
   </item>
  </Price>
 </item>
</Specification>

I tried to run a java code, but failing to merge when second code have multiple item under Specification. Running okay when second have only single item even when first have multiple Items under specification.

  • check this links : http://stackoverflow.com/questions/648471/merge-two-xml-files-in-java and http://stackoverflow.com/questions/14568995/merging-xml-file-using-java-nodelist – newuserua_ext Oct 21 '16 at 06:16
  • Just for the information, your XML 1 and XML 2 are not valid XMLs. The element type "CompositePrice" must be terminated by the matching end-tag "". – John Oct 21 '16 at 06:22
  • Thanks John for highlighting the same. – Suri2507 Oct 21 '16 at 07:32

0 Answers0