user2824073
user2824073

Reputation: 2485

Fastest (to implement) way to find pattern in an XML node

I have an XML file which contains some Nodes that needs to be varied at runtime. For example:

<pt:Type>#type</pt:Type>
<pt:Value>#value</pt:Value>

I'd like to scan the XML file and search for Nodes which have a "#" pattern in it. My purpose is to fill up the XML using some data I have in memory. Which is the fastest way to do it in Java ? Maybe is there an XPath expression to gather all nodes having a certain value ?

Upvotes: 1

Views: 194

Answers (3)

Balakrishna Nannaka
Balakrishna Nannaka

Reputation: 21

my suggestion is to go with XQuery pattern matching in java way something like this

XQExpression xqe = xqc.createExpression();
xqe.executeQuery("doc('orders.xml')//order[id='174']");

Upvotes: 1

tmarwen
tmarwen

Reputation: 16354

You can use an XPath query with the contains criteria function which would return elements that which values holds the set of character provided. It should be something like the following:

//*[contains(text(), '#')]

Upvotes: 1

ThW
ThW

Reputation: 19492

Xpath

Any node where the first text child node equals '#type':

//*[text() = '#type']

Any node where the first text child node starts with '#':

//*[starts-with(text(), '#')]

Any node where the first text child node starts with '#' after normalizing spaces:

//*[starts-with(normalize-space(text()), '#')]

Upvotes: 1

Related Questions