Qiang Li
Qiang Li

Reputation: 10855

xml xpath to match elements of either types in java

I need to match two elements of either type, how to do it in java with xpath?

For example, I have either "<?xml version='1.0' encoding='UTF-8'?><A><e1>a</e1></A>" or "<?xml version='1.0' encoding='UTF-8'?><B><e2>a</e2></B>"

And I need to match either e1 or e2.

XPathExpression expr = xpath.compile("//e1");
Object result = expr.evaluate(is, XPathConstants.NODESET);//check ((NodeList) result).getLength()

And if it does not match, I need to check similarly for e2. Is there any simpler and more efficient way, like using a regex?

Upvotes: 0

Views: 88

Answers (1)

Dhan
Dhan

Reputation: 42

You can use 'OR' operator in XPath. For example

  • (//e1 | //e2)

Upvotes: 1

Related Questions