XPath, XLink, XPointer and Java
Hello folks!
This is the first time I actually deal with XPath, XLink and Java but I didn't think I would meet so much troubles... but I hope they are trivial for you, Java gurus! :-)
Well, the issue is this:
I have an XML document with xlinks with xpointers. I have to read the xlink content (easy), load the XML source in a DOM (pretty easy), ad perform the xpath which comes from the xpointer to such linked document (mmmmhhhhhhh).
Here I have a simplified example code running on J2SE 5.0, just to execute the xpath selection:
package testproject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
publicclass XLinkParser{
/** Creates a new instance of XLinkParser */
public XLinkParser(){
}
publicstaticvoid main(String[] args){
XLinkParser xp =new XLinkParser();
String result ="";
try{
xp.performXPath(
new URL("http://www.iasf-milano.inaf.it/~luigi/projects/vo/regions/v0.1/cdfs.xml"),
"//TABLEDATA[1]/TR[5]/TD[1]");
}catch (XPathExpressionException ex){
ex.printStackTrace();
}catch (MalformedURLException ex){
ex.printStackTrace();
}catch (XPathFactoryConfigurationException ex){
ex.printStackTrace();
}catch (ParserConfigurationException ex){
ex.printStackTrace();
}catch (IOException ex){
ex.printStackTrace();
}catch (SAXException ex){
ex.printStackTrace();
}
}
publicvoid performXPath(URL xml, String xpath)throws XPathFactoryConfigurationException,
ParserConfigurationException, IOException, SAXException, XPathExpressionException{
XPathFactory xpf = XPathFactory.newInstance();
XPath xpe = xpf.newXPath();
System.err.println("Loaded XPath Provider " + xpe.getClass().getName());
// Build the source document.
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
System.err.println("Using DocumentBuilderFactory " + dfactory.getClass());
dfactory.setNamespaceAware(true);
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
System.err.println("Using DocumentBuilder " + docBuilder.getClass());
Node doc = docBuilder.parse(new InputSource(xml.openStream()));
// Compile the XPath expressions
XPathExpression findTR = xpe.compile(xpath);
// Find the nodes containing the requested path
NodeList nodes = (NodeList) findTR.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++){
System.out.println(nodes.item(i).getNodeValue());
}
}
}
It should work (IMO), returning the content of "//TABLEDATA[1]/TR[5]/TD[1]" (you may look at the source which actually exists)... bu it doesn't. Perhaps is a syntax problem... I think Xerces (which is the default implementation in J2SE 5.0 as far as I know) implements only XPath 1.0, but such expression should be compliant. Isn't it?
What am I mistaking?
Thanks in advance!

