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

ジャーナル要素は名前空間にあります。名前空間を無視することはできません。XPathと名前空間について読んでください-このフォーラムには、このテーマに関する何千もの投稿があります。

于 2012-06-25T16:37:03.043 に答える