XML 文件比较

发布于 2024-12-14 20:08:45 字数 138 浏览 1 评论 0原文

我已将 sqlite 数据库转移到 XML 文件。我正在处理数据库更新,因此当我从服务器获取一些 XML 文件时,我需要找到两个 XML 文件之间的差异,然后在我的手机数据库上应用更新。问题是如何成功比较两个xml文件?你看到什么方法了吗?

谢谢

I have transfered my sqlite database to an XML file. I am handling Database updates, so when I get some XML file from server, I need to find differences between the two XML files and then apply updates on my phone database. Problem is how to successfully compare two xml files? Do you see any approach?

Thanks

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

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

发布评论

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

评论(2

欲拥i 2024-12-21 20:08:45

难道您不能假设所有数据都是新的,并且只获取最新的 xml 文件并将所有更新应用到相关记录吗?我知道这并不优雅,但根据您的情况,这可能是一个不错的选择。

Couldn't you just assume that all the data is new and just take the most recent xml file and apply all the updates to the relevant records? I know it's not elegant but depending on your situation, might be a good bet.

酷到爆炸 2024-12-21 20:08:45

如果(且仅当)两个 XML 文件中的元素数量相同(不添加/删除行,仅更新值),您可以执行以下操作:

使用 XML 解析器(我使用 JDOM) 运行两个文件的所有元素,将一个值与另一个值进行比较。

private void compareElements( Element one, Element two ) {
   //Check if the elements have children
   if( one has children AND two has children ) {
     //Recursive call this method for each child
     List oneChildren = get list of ones children
     List twoChildren = get list of twos children
     for( Element childOne in oneChildren AND Element childTwo in twoChildren )
       compareElements( childOne, childTwo ) )
   }
   else {
      //One and two have no children and we need to compare their values
      if( one.Value != two.Value )
        //Update the value in the database
   }
}

注意:上面是用伪代码编写的,因此您必须自己计算出实际的代码 - 但我希望它给出了要做什么的一般概念。

还取决于 XML 文件的大小 - 上面的代码可能需要相当长的时间才能运行。

If (and only if) there are the same amount of elements in both XML-files (you don't add/remove rows, only update values), you could do the following:

Use an XML parser (I use JDOM) to run through all the elements of both files, comparing one value to the other.

private void compareElements( Element one, Element two ) {
   //Check if the elements have children
   if( one has children AND two has children ) {
     //Recursive call this method for each child
     List oneChildren = get list of ones children
     List twoChildren = get list of twos children
     for( Element childOne in oneChildren AND Element childTwo in twoChildren )
       compareElements( childOne, childTwo ) )
   }
   else {
      //One and two have no children and we need to compare their values
      if( one.Value != two.Value )
        //Update the value in the database
   }
}

Note: The above is written in pseudo-code, so you have to work out the actual code yourself - but I hope it gives a general idea of what to do.

Also depending on the size of your XML-files - the above could take quite some time to run.

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