使用XPath查询而不是XML解析有什么好处?

时间:2022-12-10 07:53:16

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.

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

5 个解决方案

#1


3  

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

就个人发展而言,“编写解析器”在简历中没有位置,但XPath是一种可行的有市场价值的技能。

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具有足够的通用性,可以重用:您可以对不同的XML反复使用它,提取不同的信息。如果您需要提取不同的信息,这是对查询的一个小更改,而不是对大量代码的一个大更改。

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还可以确保,如果您离开项目(或者,我不知道,会被总线撞到),下一个开发人员(他们的简历中有XPath)将能够快速、轻松地理解如何从XML中提取数据。当您将其与学习和理解您的自定义XML解析代码所花费的时间进行对比时,使用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呢?当你学习。如果您正在构建的软件不是必须可靠的专业产品,并且您之所以编写它,是因为您想要了解更多关于XML、递归或上下文无关的语法,那么请使用它并编写您自己的工具!见鬼,如果你做得很好,也许10年后我们会回答为什么人们应该使用aaaaaaaaaaaparser而不是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。

#2


2  

The number one benefit is that because XPATH-queries are already invented you don't have to manually develop (error prone) code to parse XML documents, you just have to learn the XPATH syntax :).

第一个好处是,由于XPATH查询已经发明,您不必手工开发(容易出错的)代码来解析XML文档,只需学习XPATH语法:)。

#3


2  

Writing parsers is one of the most boring and buggy practices in programming. By using a standard parsing algorithm, such as one implemented to support XPath pointing and XQuery (don't forget XQuery!) you assure to both have

编写解析器是编程中最枯燥、最麻烦的实践之一。通过使用标准解析算法,例如实现支持XPath指向和XQuery(不要忘记XQuery!),您可以保证两者都有。

  • what should be the most bug free code (please chose a good support library if the language you're using don't natively support XPath/XQuery)
  • 什么应该是最无bug的代码(如果您使用的语言没有本机支持XPath/XQuery,请选择一个好的支持库)
  • the best performances: your parser will hardly be as optimized as the native (or library) one
  • 最佳性能:您的解析器很难像本机(或库)解析器那样优化

If you need complex stuff and XPath doesn't suit all of your needs, consider XQuery.

如果您需要复杂的东西,并且XPath不能满足您的所有需求,请考虑使用XQuery。

Cheers.

欢呼。

#4


1  

In my opinion XPath is mostly easier. But it all depends on what and how much info you want to fetch. This thread has some good points: Should I use XPath or just DOM?

在我看来,XPath更简单。但这一切都取决于你想要获取的信息和数量。这个线程有一些优点:我应该使用XPath还是只使用DOM?

#5


1  

You say: "Until now I have been using XML parsing in my application"..

您会说:“到目前为止,我一直在我的应用程序中使用XML解析。”

Well, yes, however you want to process XML, the first step is always to parse it. The difference is in how you the process the data after it is parsed. There's a whole range of choice here: processing SAX events, navigating the DOM, binding to Java or C# objects, using declarative XML-specific languages such as XPath and XQuery. You don't say which of these you are currently using, or what task you are carrying out, so it's not possible to explain why (or whether) using XPath or XQuery would be better.

嗯,是的,无论您希望如何处理XML,第一步始终是解析它。不同之处在于您在解析数据之后如何处理数据。这里有很多选择:处理SAX事件、导航DOM、绑定到Java或c#对象、使用声明性的xml特定语言(如XPath和XQuery)。您不会说当前使用的是其中的哪一个,或者正在执行的任务是什么,因此无法解释为什么(或是否)使用XPath或XQuery会更好。

#1


3  

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

就个人发展而言,“编写解析器”在简历中没有位置,但XPath是一种可行的有市场价值的技能。

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具有足够的通用性,可以重用:您可以对不同的XML反复使用它,提取不同的信息。如果您需要提取不同的信息,这是对查询的一个小更改,而不是对大量代码的一个大更改。

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还可以确保,如果您离开项目(或者,我不知道,会被总线撞到),下一个开发人员(他们的简历中有XPath)将能够快速、轻松地理解如何从XML中提取数据。当您将其与学习和理解您的自定义XML解析代码所花费的时间进行对比时,使用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呢?当你学习。如果您正在构建的软件不是必须可靠的专业产品,并且您之所以编写它,是因为您想要了解更多关于XML、递归或上下文无关的语法,那么请使用它并编写您自己的工具!见鬼,如果你做得很好,也许10年后我们会回答为什么人们应该使用aaaaaaaaaaaparser而不是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。

#2


2  

The number one benefit is that because XPATH-queries are already invented you don't have to manually develop (error prone) code to parse XML documents, you just have to learn the XPATH syntax :).

第一个好处是,由于XPATH查询已经发明,您不必手工开发(容易出错的)代码来解析XML文档,只需学习XPATH语法:)。

#3


2  

Writing parsers is one of the most boring and buggy practices in programming. By using a standard parsing algorithm, such as one implemented to support XPath pointing and XQuery (don't forget XQuery!) you assure to both have

编写解析器是编程中最枯燥、最麻烦的实践之一。通过使用标准解析算法,例如实现支持XPath指向和XQuery(不要忘记XQuery!),您可以保证两者都有。

  • what should be the most bug free code (please chose a good support library if the language you're using don't natively support XPath/XQuery)
  • 什么应该是最无bug的代码(如果您使用的语言没有本机支持XPath/XQuery,请选择一个好的支持库)
  • the best performances: your parser will hardly be as optimized as the native (or library) one
  • 最佳性能:您的解析器很难像本机(或库)解析器那样优化

If you need complex stuff and XPath doesn't suit all of your needs, consider XQuery.

如果您需要复杂的东西,并且XPath不能满足您的所有需求,请考虑使用XQuery。

Cheers.

欢呼。

#4


1  

In my opinion XPath is mostly easier. But it all depends on what and how much info you want to fetch. This thread has some good points: Should I use XPath or just DOM?

在我看来,XPath更简单。但这一切都取决于你想要获取的信息和数量。这个线程有一些优点:我应该使用XPath还是只使用DOM?

#5


1  

You say: "Until now I have been using XML parsing in my application"..

您会说:“到目前为止,我一直在我的应用程序中使用XML解析。”

Well, yes, however you want to process XML, the first step is always to parse it. The difference is in how you the process the data after it is parsed. There's a whole range of choice here: processing SAX events, navigating the DOM, binding to Java or C# objects, using declarative XML-specific languages such as XPath and XQuery. You don't say which of these you are currently using, or what task you are carrying out, so it's not possible to explain why (or whether) using XPath or XQuery would be better.

嗯,是的,无论您希望如何处理XML,第一步始终是解析它。不同之处在于您在解析数据之后如何处理数据。这里有很多选择:处理SAX事件、导航DOM、绑定到Java或c#对象、使用声明性的xml特定语言(如XPath和XQuery)。您不会说当前使用的是其中的哪一个,或者正在执行的任务是什么,因此无法解释为什么(或是否)使用XPath或XQuery会更好。