简体   繁体   中英

XML parsing using SAX by xpath

i am trying to do XML parsing using SAX by using xpath. but when i try for getting data for multiple nodeset it do not give that.

  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;

  import javax.xml.xpath.XPath;
  import javax.xml.xpath.XPathConstants;
  import javax.xml.xpath.XPathExpression;
  import javax.xml.xpath.XPathExpressionException;
  import javax.xml.xpath.XPathFactory;

  import org.apache.xpath.NodeSet;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;


  public class XPathEvaluator{


   public void evaluateDocument(File xmlDocument){

    try{
 XPathFactory factory=XPathFactory.newInstance();
 XPath xPath=factory.newXPath();
 InputSource inputSource=new InputSource(new FileInputStream(xmlDocument));
 XPathExpression         

  xPathExpression=xPath.compile("/catalog/journal/article[@date='January-2004']/title");
String title=xPathExpression.evaluate(inputSource);
System.out.println("Title: "+ title);

 inputSource=new InputSource(new FileInputStream(xmlDocument));
String publisher=xPath.evaluate("/catalog/journal/@publisher", inputSource);
System.out.println("Publisher:"+ publisher);    

String expression="/catalog/journal[@title='Java Technology']/article";
NodeSet nodes = (NodeSet) xPath.evaluate(expression, inputSource,XPathConstants.NODESET);    
NodeList nodeList=(NodeList)nodes;   
System.out.println("node List"+nodeList);
  }
  catch(IOException  e){}
 catch(XPathExpressionException e){}

 }


 public static void main(String[] argv){


 XPathEvaluator evaluator=new XPathEvaluator();

 File xmlDocument=new File("e://catalog-modified.xml");
 evaluator.evaluateDocument(xmlDocument);

 }

  }

my catalog-modified.xml is as follows

       <?xml version="1.0" encoding="UTF-8"?>
    <catalog xmlns:journal="http://www.w3.org/2001/XMLSchema-Instance"> 
        <journal:journal title="XML" publisher="IBM  developerWorks">       
    <article journal:level="Advanced"   date="February-2003">   
              <title>Design XML Schemas Using UML</title>          
              <author>Ayesha Malik</author>   
    </article>     
</journal:journal> 
 <journal title="Java Technology" publisher="IBM     
      developerWorks">             
 <article level="Intermediate"  date="January-2004" 
      section="Java Technology">                      
   <title>Service Oriented Architecture Frameworks
       </title>                  
       <author>Naveen Balani       
       </author>               
    </article>  
    <article level="Advanced" date="October-2003"  section="Java Technology">                 
       <title>Advance DAO Programming</title>                   
      <author>Sean Sullivan</author>               
   </article>    
    <article level="Advanced" date="May-2002"  section="Java Technology">     
       <title>Best Practices in EJB Exception Handling   </title>          
       <author>Srikanth Shenoy      
       </author>      
     </article> 
</journal> 

if i try to call this do not show any nodeset for this .

The journal element is in a namespace. You can't ignore the namespace. Please read about XPath and namespaces - there are thousands of posts on the subject in this forum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM