[jdom-interest] questions on using XPath.newInstance() and expr

Mikael Petterson (KI/EAB) mikael.petterson at ericsson.com
Fri Jan 30 02:21:53 PST 2004


Hi,

I downloaded jdom-9.

I looked at the example (jdom-b9/samples) called:

XPathReader.java
--------------------------

XPath servletPath = XPath.newInstance("//servlet");


To my questions:

Q1:Is it possible to only create one Instance of XPath and just use different expressions when navigating the xml document. That is:

//Global for the class that use it
public static XPath path;

//First time I create XPath
 path = new XPath.newInstance("<myexpression1>");
......

//Next time I use it, in same class, I do:

List cars = path.selectNodes(doc,"<myexpression2>");

Q2: How can I add a java String variable to an XPath expression? Example:

    String parent = "John";
    String query = "//Foundation.Core.Class[Foundation.Core.ModelElement.name='"+parent+"']/Foundation.Core.Gene
ralizableElement.specialization"

   How can I make sure that query is interpreted as:

   //Foundation.Core.Class[Foundation.Core.ModelElement.name='"John"]/Foundation.Core.Gene
ralizableElement.specialization"


Regards,

//Mikael



More information about the jdom-interest mailing list