1

我正在尝试通过使用 xpath 使用 SAX 进行 XML 解析。但是当我尝试获取多个节点集的数据时,它没有给出。

  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);

 }

  }

我的 catalog-modified.xml 如下

       <?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> 

如果我尝试调用它,请不要为此显示任何节点集。

4

1 回答 1

1

journal 元素位于命名空间中。您不能忽略命名空间。请阅读有关 XPath 和名称空间的信息——在这个论坛上有数千篇关于该主题的帖子。

于 2012-06-25T16:37:03.043 回答