xalan namespace problem

Discussion in 'XML' started by lancelot_234, May 27, 2010.

  1. lancelot_234

    lancelot_234

    Joined:
    May 27, 2010
    Messages:
    1
    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

    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.
    lancelot_234, May 27, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobias Schierge

    Xalan document() memory problem

    Tobias Schierge, Feb 3, 2004, in forum: Java
    Replies:
    1
    Views:
    483
    Christian
    Feb 9, 2004
  2. Elpecek

    JBoss and Xalan problem ...

    Elpecek, Nov 2, 2004, in forum: Java
    Replies:
    3
    Views:
    1,176
    John C. Bollinger
    Nov 3, 2004
  3. Mark F
    Replies:
    0
    Views:
    449
    Mark F
    Mar 7, 2005
  4. Felix Engelhardt

    JAXP / Xalan / data-types problem

    Felix Engelhardt, Apr 17, 2006, in forum: Java
    Replies:
    1
    Views:
    625
    Felix Engelhardt
    Apr 18, 2006
  5. cvissy
    Replies:
    0
    Views:
    603
    cvissy
    Nov 16, 2004
Loading...

Share This Page