XPATH java query (urgent)

I am trying to get list of titles from this xml using this programe j2se 5 but i am not getting any results can any one help?
----------------class one for Xpath
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class pratcice {
      static Document doc;
     
      public static void main(String[] args)  {
            try{
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              DocumentBuilder parser = factory.newDocumentBuilder();
             doc = parser.parse("http://depot.cnx.rice.edu:8080/content/OAI?verb=SearchRecords&metadataPrefix=ims1_2_1&query:list=dsp%20and%20C%20language&b_size=-1");
            XPath xpath = XPathFactory.newInstance().newXPath();
            PersonalNamespaceContext      nsctx = new PersonalNamespaceContext("ims1_2_1", "http://cooker.cnx.rice.edu:8080/content/OAI");
            xpath.setNamespaceContext(nsctx);
            // Evaluate the document against the pattern
      //      NodeList nodeList = (NodeList) xpath.evaluate("//identifier/text()", doc, XPathConstants.NODESET);
            //NodeList nodeList = (NodeList) xpath.evaluate("//record/metadata/*", doc, XPathConstants.NODESET);
         

      NodeList nodeList =  (NodeList) xpath.evaluate("//ims1_2_1:title/ims1_2_1:langstring",   doc, XPathConstants.NODESET);
            System.out.println(nodeList.getLength());
            if (nodeList.getLength() > 0) {
                  for(int i = 0;i < nodeList.getLength();i++) {
                        System.out.println(nodeList.item(i).getNodeValue());  
                  }
            }      
       } catch (Exception e){e.printStackTrace();}
      }
}
-----------------orielly class-------
package mpkg;


import java.util.Iterator;
import javax.xml.*;
import javax.xml.namespace.NamespaceContext;

public class PersonalNamespaceContext implements NamespaceContext{



       public String uri;

       public String prefix;



       public PersonalNamespaceContext(){}



       public PersonalNamespaceContext(String prefix, String uri){

           this.uri=uri;

           this.prefix=prefix;

          }



       public String getNamespaceURI(String prefix){

         return uri;

       }

       public void setNamespaceURI(String uri){

         this.uri=uri;

       }



       public String getPrefix(String uri){

         return prefix;

       }



       public void setPrefix(String prefix){

         this.prefix=prefix;

       }

       public Iterator getPrefixes(String uri){return null;}



      }
LVL 2
mostenscerAsked:
Who is Participating?
 
kawasConnect With a Mentor Commented:
Hi, one small thing was that you didnt set the factory to be namespace aware. also, your namespace uri / prefix didnt match. Here is code that is modified and works.

Good luck


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class pratcice {
      static Document doc;
     
      public static void main(String[] args)  {
            try{
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true);
              DocumentBuilder parser = factory.newDocumentBuilder();
             doc = parser.parse("http://depot.cnx.rice.edu:8080/content/OAI?verb=SearchRecords&metadataPrefix=ims1_2_1&query:list=dsp%20and%20C%20language&b_size=-1");
             XPathFactory factory2 = XPathFactory.newInstance();
             XPath xpath = factory2.newXPath();
             xpath.setNamespaceContext(new PersonalNamespaceContext());
             XPathExpression expr
               = xpath.compile("//ims1_2_1:title/ims1_2_1:langstring");

             Object result = expr.evaluate(doc, XPathConstants.NODESET);
             NodeList nodes = (NodeList) result;
             System.out.println(nodes.getLength());
             for (int i = 0; i < nodes.getLength(); i++) {
                   System.out.println(nodes.item(i).getTextContent());
             }  
       } catch (Exception e){e.printStackTrace();}
      }
}

-------------------------------------
import java.util.Iterator;
import javax.xml.*;
import javax.xml.namespace.NamespaceContext;

public class PersonalNamespaceContext implements NamespaceContext{



       public String uri = "http://www.imsglobal.org/xsd/imsmd_v1p2";

       public String prefix = "ims1_2_1";



       public PersonalNamespaceContext(){}



       public String getNamespaceURI(String prefix){
             if (prefix == null) throw new NullPointerException("Null prefix");
           else if ("ims1_2_1".equals(prefix)) return "http://www.imsglobal.org/xsd/imsmd_v1p2";
             System.out.println(prefix);
           return XMLConstants.NULL_NS_URI;
       }

       public void setNamespaceURI(String uri){

         this.uri=uri;

       }



       public String getPrefix(String uri){

         return prefix;

       }



       public void setPrefix(String prefix){

         this.prefix=prefix;

       }

       public Iterator getPrefixes(String uri){return null;}



      }

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.