XPathFactory not working

I am trying to use XPathFactory to evaluate an expression in a Java application. But I get a Saxon error. At one time I used Saxon for some functionality, and for this I had to set the system property:

System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl"); XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); 

However, now I just want to do some XML processing using the default DOM (org.w3c.dom.Document) and process it using xpath, so Saxon is not needed. But when I try to create XPathFactory, I still get the Saxon error message:

 Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: net/sf/saxon/lib/EnvironmentVariableResolver at net.sf.saxon.xpath.XPathFactoryImpl.<init>(XPathFactoryImpl.java:26) ... 

I even tried to "reset" the system property:

 System.setProperty("javax.xml.xpath.XPathFactory:", "org.apache.xpath.jaxp.XPathFactoryImpl"); XPathFactory factory = XPathFactory.newInstance(); 

and

 System.setProperty("javax.xml.xpath.XPathFactory:", "http://java.sun.com/jaxp/xpath/dom"); XPathFactory factory = XPathFactory.newInstance(); 

But this does not help, I still get the same error message.

How do I get rid of this to use XPathFactory by default again? (this worked fine before I tried using the saxon version)

+6
source share
2 answers

As a workaround, you can explicitly install the JDK factory (either Xerces or Saxon's).

 import org.apache.xpath.jaxp.XPathFactoryImpl // import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl // import net.sf.saxon.xpath.XPathFactoryImpl ... XPathFactory factory = new XPathFactoryImpl(); 

If possible, prefer a real Xerces implementation to the one found in the JDK. It is more reliable.

+1
source

I came across the same question. Even "System.setProperty" is not called, jaxp will load the saxon xpath engine, since the default implementation of saqon jar is in the classpath. Ref: namespace-unaware XPath expression fails if Saxon is in CLASSPATH .

My solution: call saxon directly as: "XPathFactory _xFactory = new net.sf.saxon.xpath.XPathFactoryImpl ();" and add jaxen-xxx.jar and xercesImpl.jar before saxon9e.jar in the classpath. Everything else remains in its original state without calling System.setProperty. This works for me.

I also test the following method as follows:

  System.setProperty("javax.xml.xpath.XPathFactory:" +XPathConstants.DOM_OBJECT_MODEL, "net.sf.saxon.xpath.XPathFactoryImpl"); XPathFactory xFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME +":" + XPathFactory.DEFAULT_OBJECT_MODEL_URI, " org.apache.xpath.jaxp.XPathFactoryImpl"); XPathFactory xFactory2 = XPathFactory.newInstance(); System.out.println(xFactory.toString()); System.out.println(xFactory2.toString()); 

Exit: net.sf.saxon.xpath.XPathFactoryImpl@71623278 com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl@768b970c Since Jaxp uses apache jaxen as its default xpath implementation, this method should work. Because JAXP uses

+1
source

All Articles