How to get a list of String from Dom4j Node.selectObject or Node.selectNodes

*爱你&永不变心* 提交于 2019-12-24 02:27:09

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/4628647/how-to-get-a-list-of-string-from-dom4j-node-selectobject-or-node-selectnodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!