比较两个 xml 文件中的特定元素 -- 在 java 中

发布于 2024-11-15 17:08:36 字数 996 浏览 0 评论 0原文

我尝试尽可能长时间地避免问这个问题......但我还没有找到任何可以完美满足我的需求的东西,现在是我完成这个项目的时候了。

所以这就是我所需要的:

我正在制作一个应用程序,它基本上可以比较两个 xml 文档,然后对其中一个进行微小的更改。我有一个模板xml,然后其他xml文件每次都会改变。我只需确定模板中的关键元素是否在另一个文件中。任何额外的内容都可以忽略,但缺少的关键元素应该添加。

视觉表示:

模板

<root attribute="asd">
    <a />
    <b>
        <c asdf="fdsa" />
    </b>
</root>

其他 xml

<root>
    <a d="wer" />
    <b>
        <c />
    <b>
    <d />
</root>

最终结果:

<root attribute="asd">
    <a d="wer" />
    <b>
        <c asdf="fdsa" />
    </b>
    <d />
</root>

我的问题:

有什么方法可以完成这个最终结果,而无需费力地检查每个关键元素并确保它们存在?我研究过 XPath,这似乎是迄今为止我最好的解决方案,只需检查是否存在必要的部分,然后添加它们(如果不存在)(我将创建一个新文件,因此用新的元素并不是很麻烦)。但是,由于我必须检查很多事情,所以我宁愿不这样做。我还查看了 XMLUnit 和 XOM,但它们似乎更适合比较和告诉我差异(包括添加了额外的行,我对此不感兴趣)。

那么,有什么好主意来实现两行代码并让它神奇地完成自己吗?因为那太好了:)

I've tried avoiding asking this question for as long as possible... but I haven't found anything that will fit my needs perfectly, and it's about time I get this project done.

So here's what I need:

I'm making an app that will basically diff two xml documents, and then make minor changes to one of them. I have a template xml, and then the other xml file will change each time. I only have to determine whether or not the key elements from the template are in the other file. Any extra stuff can be ignored, but missing key elements should be added.

Visual representation:

template

<root attribute="asd">
    <a />
    <b>
        <c asdf="fdsa" />
    </b>
</root>

other xml

<root>
    <a d="wer" />
    <b>
        <c />
    <b>
    <d />
</root>

final result:

<root attribute="asd">
    <a d="wer" />
    <b>
        <c asdf="fdsa" />
    </b>
    <d />
</root>

My question:

Is there any way to accomplish this final result without painstakingly going through every key element and making sure they exist? I've looked into XPath, and this seems like my best solution so far, simply checking to see if necessary parts are present, and then adding them if they aren't (I'd be making a new file, so writing in new elements isn't terribly troublesome). However, since there are a LOT of things I'd have to check for, I'd rather not do this. I also looked at XMLUnit and XOM, but those seem better suited to comparing and telling me the differences (including where extra lines have been added, which I'm not interested in).

So, any great idea to implement two lines of code and have this just magically accomplish itself? Cuz that would be great :)

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

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

发布评论

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

评论(1

终遇你 2024-11-22 17:08:36

我想,您应该能够使用 XSLT 来做到这一点。不是两行代码 - 根本没有代码,只是 XSLT 文件中合并启发式的适当定义。

请参阅此处讨论和解决的类似问题。

You should be able to do this using XSLT, I would have thought. Not two lines of code - no code at all, just a suitable definition of the merge heuristics in your XSLT file.

See similar issue discussed and solved here.

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