如何从 Dom4j Node.selectObject 或 Node.selectNodes 获取字符串列表

发布于 2024-10-10 21:27:33 字数 705 浏览 0 评论 0原文

希望您有美好的一天。

Dom4j javadoc 形式 Node.selectObject(String xpathExpression) 表示如下:

...返回的对象可以是一个或多个 Node 实例的列表,也可以是一个标量对象,例如 >String 或 Number 实例,具体取决于 XPath 表达式。

获取这段 xml 上的 String 列表时:

<root>
...
    <level1>
        <property>pro1</property>
        <property>pro1</property>
        <property>pro1</property>
    <level1>
...
</root>

但是,当我尝试使用以下代码

List result = document.selectObject("/root/level1/property/text()")

我得到了 org 的列表.dom4j.tree.DefaultText 对象。当然,我可以迭代列表,但我想知道是否有办法获取字符串列表。

Hope you have a good day.

The Dom4j javadoc form Node.selectObject(String xpathExpression) says the following:

...The object returned can either be a List of one or more Node instances or a scalar object like a String or a Number instance depending on the XPath expression.

However when I try to get a list of String on this piece of xml:

<root>
...
    <level1>
        <property>pro1</property>
        <property>pro1</property>
        <property>pro1</property>
    <level1>
...
</root>

with the following code:

List result = document.selectObject("/root/level1/property/text()")

I get a list of org.dom4j.tree.DefaultText objet. Of course I can iterate on the list but I yould like to know if there is a way to get a list of String.

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

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

发布评论

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

评论(1

一个人的夜不怕黑 2024-10-17 21:27:33

来自 http://www.w3.org/TR/xpath/#section-Introduction< /a>

对表达式进行求值以产生
对象,具有以下之一
四种基本类型:

  • 节点集(无重复的无序节点集合)
  • 布尔值(真或假)
  • 数字(浮点数)
  • 字符串(UCS 字符序列)

因此,没有 xs:string< 序列XPath 1.0 中的 /code>

在 XPath 2.0 中,当然有一个序列数据类型......

From http://www.w3.org/TR/xpath/#section-Introduction

An expression is evaluated to yield an
object, which has one of the following
four basic types:

  • node-set (an unordered collection of nodes without duplicates)
  • boolean (true or false)
  • number (a floating-point number)
  • string (a sequence of UCS characters)

So, no sequence of xs:string in XPath 1.0

In XPath 2.0 there is a sequence data type, of course...

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