java合并xml文件

发布于 2024-11-29 15:09:16 字数 2091 浏览 1 评论 0原文

我有两个不同的 xml 文件,如下所述,想要合并这些 xml 文件并获得预期的输出,可能使用 xpath 或 dom 解析而不是 XSLT,因为 xml 始终不相同

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>

感谢进步 ....

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 ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂ガ小子 2024-12-06 15:09:16

如果您可以灵活地创建新的 xml 文件,则可以使用您熟悉的任何解析器来解析每个文件。将标签存储在字符串 LinkedList 的 LinkedList 中,并将标签值存储在以下类型的 HashMap 中:
LinkedHashMap 数据= new LinkedHashMap();

然后,您可以从链接列表中调用标记名称,附加哈希映射中的标记值并将它们写入新的 XML 文件。
当我合并 XML 时,这就是我使用的过程。
希望这有帮助

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

忘羡 2024-12-06 15:09:16
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"));

}

这将获取哈希映射中的标签值和链表中的标签名称。为了使您的工作更轻松,您可以将标签的类型附加到哈希映射键。例如:如果我的密钥是员工 ID(在我的例子中),我会在其后面附加“first”。假设某个人的 id:10001。他的数据将存储为:10001,然后是 10001first、10001last、10001loc、10001occ。现在,您可以调用每个 hashmap 键,根据附加的标签名称获取元素并连接到您的 xml 文件。
希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文