开发者

What is the benefit of using XPath queries instead of XML parsing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question 开发者_Python百科

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

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


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

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.

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.

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.

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.


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

  • 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)
  • 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.

Cheers.


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 :).


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜