有没有办法使用 Scala 的 XML 库执行 XPath 字符串查询?
给定一个 scala XML 对象,我可以执行像“//entries[@title='scala']”这样的 xpath 字符串查询吗?
理想情况下,它会像:
我无法手动将所有 xpath 查询转换为 scala 的内部 xpath-ish 方法调用,因为我的程序将动态接受 xpath 查询。
此外,内置的 java xml 库非常冗长,所以我想避免它。
Given an scala XML object, can I perform a xpath string query like "//entries[@title='scala']" ?
Ideally, it would be like:
<a><b name='n1'></b></a>.xpath("//b[@name='n1']")
I can't manually convert all the xpath queries to scala's internal xpath-ish method calls as my program will is dynamically accepting xpath queries.
Additionally, the built-in java xml library is very verbose so I would like to avoid it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的选择是(而且一直都是,即使是在 Java 中)使用 JDOM。我用下面的库对 JDom 进行了拉皮条,使其对 scala 更加友好:
当我写这篇文章时,我的想法是,一般来说,您希望提前编译每个 XPath 查询(以便可以多次重用它),并且您希望在指定查询文本时指定查询返回的类型(与 JDOM 的 XPath 类不同,后者在执行时选择四种方法之一进行调用)。
命名空间应该隐式传递(这样您就可以指定它们一次,然后就忘记它们),并且 XPath 变量绑定应该在查询时可用。
您可以像这样使用该库:(可以推断出显式类型注释 - 我将它们包含在内只是为了说明。)
我想我应该想出某种方法将其发布给公众。
Your best bet is (and always was, even in Java) to use JDOM. I've pimped JDom with the following library to be a bit more scala friendly:
My idea when I wrote this was that in general, you want to compile each XPath query in advance (so that it can be reused more than once), and that you want to specify the type returned by the query at the point where you specify the text of the query (not like JDOM's XPath class does which is to pick one of four methods to call at execution time).
Namespaces should be passed around implicitly (so you can specify them once and then forget about them), and XPath variable binding should be available at query time.
You'd use the library like this: (Explicit type annotations can be inferred -- I've included them for illustration only.)
I guess I should come up with some way of releasing this to the public.
kantan.xpath 就是这样做的。这是我刚刚在 REPL: 中输入的内容
,其中
Node
类型参数描述了期望从 XML 文档中提取的类型。一个可能更明确的例子是:这将下载 stackoverflow 主页,将其评估为 XML 文档(有一个用于 HTML 清理的 NekoHTML 模块)并提取所有链接的目标。
kantan.xpath does just that. Here's something I just typed in the REPL:
, where the
Node
type parameter describes the type one expects to extract from the XML document. A perhaps more explicit example would be:This would download the stackoverflow homepage, evaluate it as an XML Document (there's a NekoHTML module for HTML sanitisation) and extract the target of all links.