J
Joona I Palaste
In our application's code, we need to use XPath expressions to fetch
data from XML documents. I'm using the Apache Xalan XPath implementation
to do this.
The only thing I've not figured out is how to resolve variables. How
can I set an XPath variable to some value from the Java code?
For example:
XML code:
<document>
<element name="foo">This is an element</element>
<element name="bar">This is also an element</element>
</document>
Java code (simplified):
Document document = getTheDocument();
//what to write here???
NodeIterator nl = XPathAPI.selectNodeIterator(document,
"/document/element[name=$var]/text()");
This kind of code, as is, throws a RuntimeException about an
unresolved variable "var". Of course it's unresolved, because I don't
know how to resolve it!
The part that says "//what to write here???" should set the
variable "var" to "foo" so that the node iterator would find this
node:
This is an element
How can I do this? Thanks for any help!
data from XML documents. I'm using the Apache Xalan XPath implementation
to do this.
The only thing I've not figured out is how to resolve variables. How
can I set an XPath variable to some value from the Java code?
For example:
XML code:
<document>
<element name="foo">This is an element</element>
<element name="bar">This is also an element</element>
</document>
Java code (simplified):
Document document = getTheDocument();
//what to write here???
NodeIterator nl = XPathAPI.selectNodeIterator(document,
"/document/element[name=$var]/text()");
This kind of code, as is, throws a RuntimeException about an
unresolved variable "var". Of course it's unresolved, because I don't
know how to resolve it!
The part that says "//what to write here???" should set the
variable "var" to "foo" so that the node iterator would find this
node:
This is an element
How can I do this? Thanks for any help!