使用 XPath 查询而不是 XML 解析有什么好处? [英] What is the benefit of using XPath queries instead of XML parsing?

查看:29
本文介绍了使用 XPath 查询而不是 XML 解析有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在我的应用程序中使用 XML 解析,但我最近遇到了 XPath 查询的使用.

Until now I have been using XML parsing in my application, but I recently came across the use of XPath queries.

我的问题是,从 XML 获取信息的更好方法是什么?什么情况下应该使用XPath查询.

My question is, what is the better way to get the information from the XML? Under which circumstances should XPath queries be used.

推荐答案

就你的个人发展而言,编写解析器"在简历中没有地位,但 XPath 是一项可行的市场技能.

In terms of your personal development, "writing parsers" has no place on a CV, but XPath is a viable marketable skill.

这是因为 XPath 具有足够的通用性,可以重用:您可以对不同的 XML 反复使用它,提取不同的信息.如果您需要提取不同的信息,只需对查询进行小幅更改,而不是对大量代码进行大更改.

This is because XPath is sufficiently generic to be reusable: you can use it over and over again for different XML, extracting different information. If you need to extract different information, it's a small change to a query as opposed to a large change to a huge chunk of code.

使用 XPath 还可以确保如果您离开项目(或者,我不知道,被公共汽车撞到)下一个开发人员(他们的简历上有 XPath)将快速轻松地了解如何从 XML 中提取数据.当您将其与他们学习和理解您的自定义 XML 解析代码所需的时间进行对比时,使用 XPath 更适合您的团队.

Using XPath also ensures that if you leave the project (or, I dunno, get hit by a bus) the next developer (who has XPath on their CV) will quickly and easily understand how to extract data from the XML. When you contrast that with the time it will take them to learn and understand your custom XML parsing code, using XPath is better for you team.

那么,您什么时候不使用 XPath?学习时.如果您正在构建的软件不是必须可靠的专业产品,并且您正在编写它是因为您想更多地了解 XML、递归或上下文无关语法,那么就去编写自己的工具吧!见鬼,如果你做得很好,也许 10 年后我们就会回答关于为什么人们应该使用 AAAAAAAAAParser 而不是 XPath 的问题.

So, when do you not use XPath? When youre learning. If the software you're building isn't a professional product which has to be reliable, and you're writing it because you want to understand more about XML, recursion or context-free grammars, go for it and write your own tool! Hell, if you do a good job of it, maybe 10 years from now we'll be answering questions about why people should use AAAAAAAAAParser instead of XPath.

但是,如果您因交付产品而获得报酬;如果您与团队中的其他开发人员一起工作;如果有可能有一天您将这些代码抛在脑后而其他人将维护它,那么请使用 XPath.

But if you're getting paid to deliver a product; if you're working with other developers on a team; if there's a chance that someday you will leave this code behind and someone else will be maintaining it, then use XPath.

这篇关于使用 XPath 查询而不是 XML 解析有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆