troubleshooting Question

XPATH java query (urgent)

Avatar of mostenscer
mostenscer asked on
Programming Languages-OtherXMLJava EE
1 Comment1 Solution590 ViewsLast Modified:
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;}



      }
ASKER CERTIFIED SOLUTION
kawas

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros