java合并xml文件
我有两个不同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以灵活地创建新的 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
这将获取哈希映射中的标签值和链表中的标签名称。为了使您的工作更轻松,您可以将标签的类型附加到哈希映射键。例如:如果我的密钥是员工 ID(在我的例子中),我会在其后面附加“first”。假设某个人的 id:10001。他的数据将存储为:10001,然后是 10001first、10001last、10001loc、10001occ。现在,您可以调用每个 hashmap 键,根据附加的标签名称获取元素并连接到您的 xml 文件。
希望这有帮助。
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.