比较一个 xml 中的元素并将其添加到另一个 xml

发布于 2024-10-19 23:40:52 字数 715 浏览 5 评论 0原文

我有两个 xml 文件

XML1

 <node>
    <name>abc</name>
    <age>25</age>
  </node>

XML2

 <node>
    <name>abc</name>
    <age>25</age>
  </node>
  <node>
    <name>xyz</name>
    <age>27</age>
  </node>

我想比较这两个文件并将缺少的节点从 XML2 添加到 XML1。 我在 C++ 中使用tinyxml。

我正在考虑向每个节点添加一个 guid,即

<node id={732C8F52-D3E2-4929-9199-48F8F38EA5D3}>
    <name>xyz</name>
    <age>27</age>
  </node>

现在当我进行比较时,我可以循环遍历 xml 中的节点并根据 id 比较节点并将缺少的节点添加到 XML1 中。

有没有更好的方法来实现这一点?

I have two xml files

XML1

 <node>
    <name>abc</name>
    <age>25</age>
  </node>

XML2

 <node>
    <name>abc</name>
    <age>25</age>
  </node>
  <node>
    <name>xyz</name>
    <age>27</age>
  </node>

I want to compare these two files and add missing nodes from XML2 to XML1.
I am using tinyxml in C++.

I am thinking of adding a guid to each node ie

<node id={732C8F52-D3E2-4929-9199-48F8F38EA5D3}>
    <name>xyz</name>
    <age>27</age>
  </node>

Now when I will compare I can loop through nodes in both xml and compare nodes based on id and add the missing nodes into XML1.

Is there any better way to accomplish this?

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

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

发布评论

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

评论(1

歌枕肩 2024-10-26 23:40:52

使用 XML 解析库当然更好,例如 Property_Tree Boost 存储库中的库,TinyXMLpugixml > - 据说非常强大。

您必须能够根据上述提供的功能找到一些比较器(或简单地构建一个比较器)并比较您的记录。

使用 Boost 读取和写入 XML 文件了解更多信息。

It certainly is better to use an XML parsing library, e.g. one given in the Property_Tree library in the Boost Repository, TinyXML or pugixml - which is said to be very powerful.

You must be able to find some comparator (or build one simply) based on the functionalities provided by the above and compare your records.

Using Boost to read and write XML files for more.

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