- Joined
- May 27, 2010
- Messages
- 1
- Reaction score
- 0
Hi,
I'm trying to parse a xml file (that has namespace) using Xalan library.
I'm using the following files/code (from the example about namespace context in: xml.apache.org) :
- foo.xml
- MyNamespaceContext.java
(unfortunately, I can't put foo.xml or MyNamespaceContext.java in this post)
this is the code I'm using to parse the xml file:
XpathExampleFoo.java
when I execute "XpathExampleFoo", the result is: "null"
could somebody tell me what is wrong with the code?
thanks.
I'm trying to parse a xml file (that has namespace) using Xalan library.
I'm using the following files/code (from the example about namespace context in: xml.apache.org) :
- foo.xml
- MyNamespaceContext.java
(unfortunately, I can't put foo.xml or MyNamespaceContext.java in this post)
this is the code I'm using to parse the xml file:
XpathExampleFoo.java
Code:
import java.io.IOException;
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class XpathExampleFoo {
public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
XpathExampleFoo a = new XpathExampleFoo();
a.analyzeXml();
} // end main
public void analyzeXml() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("foo.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new MyNamespaceContext() );
XPathExpression expr
= xpath.compile("/foo:document/bar:element");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
} // end method
} // end class
when I execute "XpathExampleFoo", the result is: "null"
could somebody tell me what is wrong with the code?
thanks.