Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have a small problem in using regular expressions along with xml and xpath.

I have an xml file like this

messages.xml

<message>
  <text>dog goes woof</text>
</message>
<message>
  <text>cat goes meow, dog goes woof, fish goes blub</text>
</message>

and then i have an xpath expression which allows me to select the text node which has text node as dog goes woof like this

   String expression = "//text[.='dog goes woof']";
   NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(messages, XPathConstants.NODESET);

here "messages" is the Document variable which refers to the messages.xml file.

when i iterate through the nodeList, it selects only the first text node. I want to select the other text node too which contains dog goes woof. So how can i specify in the xpath expression to check for text nodes which contains dog goes woof in them.

please let me know how to do it.

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

you can use the contains() function or if you really want to use regex you can use the matches() function

"//text[contains(text(), 'dog goes woof')]"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...