开发者

Merging of xml files java

I have two different xml files described as below and want to merge these xml files and get the expected output may be using xpath or dom parsing but not XSLT since the xmls are always not the same

XML1.xml

<personinfo>
   <person>
     <name><name>
     <age></age>
     <address>
     <street></street>
     <city></city>
     <address>
   </person>
   <person>
     <name><name>
     <age></age>
     <address>
     <street></street>
     <city></city>
     <address>
 开发者_高级运维  </person>
   <person>
     <name><name>
     <age></age>
     <address>
     <street></street>
     <city></city>
     <address>
   </person>
</personinfo>

XML2.xml

<personinfo>
   <person>
     <name>tom<name>
     <age>26</age>
     <address>
     <street>main street</street>
     <city>washington</city>
     <address>
   </person>
   <person>
     <name>mike<name>
     <age>30</age>
     <address>
     <street>first street</street>
     <city>dallas</city>
     <address>
   </person>
</personinfo>

Expected.xml

<personinfo>
   <person>
     <name>tom<name>
     <age>26</age>
     <address>
     <street>main street</street>
     <city>washington</city>
     <address>
   </person>
   <person>
     <name>mike<name>
     <age>30</age>
     <address>
     <street>first street</street>
     <city>dallas</city>
     <address>
   </person>
   <person>
     <name><name>
     <age></age>
     <address>
     <street></street>
     <city></city>
     <address>
   </person>
</personinfo>

Thanks in advance ....


If you have the flexibility to create a new xml file, you can parse each of them using any parser you are comfortable with. Store the tags in a LinkedList of String LinkedLists and the tag values in a HashMap of the following type: LinkedHashMap data= new LinkedHashMap();

You can then call the tag names from the linked lists, append the tag values from the Hash Map and write them out to a new XML file. When I did merging of XMLs, this was the procedure I used. Hope this helps


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("employee");
for (int s = 0; s < nodeLst.getLength(); s++) 
{
stkey=getXMLData(s,nodeLst,"id");
     keylist.add(stkey);// adding integer keys to a Linked List
data.put(stkey, stkey);                 
data.put(stkey+"first",getXMLData(s,nodeLst,"firstname"));                  
data.put(stkey+"last",getXMLData(s,nodeLst,"lastname"));                    
     data.put(stkey+"loc",getXMLData(s,nodeLst,"location"));    
     data.put(stkey+"occ",getXMLData(s,nodeLst,"occupation"));

}

this will get the tag values in the hash map and the tag names in the linked list. to make your work easier, you can append the type of tag to the hashmap key. For example: if my key is the Employee ID(in my case), I append "first" to it. Lets say some one has an id: 10001. his data would be stored as: 10001, then 10001first, 10001last, 10001loc,10001occ. Now, you can call each hashmap key, get the element as per appended tag name and concatenate to your xml file. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜