2014-07-08 78 views
0

這裏是我的xml文件:bookstore.xml,我想要做的是我提供xpath作爲輸入,它應該給我它的節點值。但它給了我空值。用xpath檢索xml節點值

<?xml version="1.0" encoding="UTF-8"?> 
<bookstore> 
    <book category="cooking"> 
    <title lang="en">Everyday Italian</title> 
    <author>Giada De Laurentiis</author> 
    <year>2005</year> 
    <price>30.00</price> 
    </book> 
    <book category="children"> 
    <title lang="en">Harry Potter</title> 
    <author>J K. Rowling</author> 
    <year>2005</year> 
    <price>29.99</price> 
    </book> 
    <book category="web"> 
    <title lang="en">XQuery Kick Start</title> 
    <author>James McGovern</author> 
    <author>Per Bothner</author> 
    <author>Kurt Cagle</author> 
    <author>James Linn</author> 
    <author>Vaidyanathan Nagarajan</author> 
    <year>2003</year> 
    <price>49.99</price> 
    </book> 
    <book category="web" cover="paperback"> 
    <title lang="en">Learning XML</title> 
    <author>Erik T. Ray</author> 
    <year>2003</year> 
    <price>39.95</price> 
    </book> 
</bookstore> 

這裏是Java代碼:

public class XmlParserUsingXpath { 


     public void xmlParser(XPathExpression xpathexp) throws ParserConfigurationException, XPathExpressionException{ 


      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 

      DocumentBuilder dbuilder = factory.newDocumentBuilder(); 

      Document doc=null; 

      try { 
       doc= dbuilder.parse(new FileInputStream("D:\\Bookstore.xml")); 

      } catch (SAXException e) { 

       e.printStackTrace(); 

      } catch (IOException e) { 

       e.printStackTrace(); 
      } 

      doc.getDocumentElement().normalize(); 

      Object result = xpathexp.evaluate(doc, XPathConstants.NODESET); 

      NodeList nodes = (NodeList) result; 
      for (int i = 0; i < nodes.getLength(); i++) { 
       System.out.println(nodes.item(i)); 
      } 

     public static void main(String[] args){ 

      XmlParserUsingXpath o1= new XmlParserUsingXpath(); 

      XPath x=XPathFactory.newInstance().newXPath(); 

      System.out.println("Enter the Xpath Expression : "); 

      Scanner sc= new Scanner(System.in); 

      String scan= sc.nextLine(); 

      try { 

       o1.xmlParser(x.compile(scan)); 

      } catch (XPathExpressionException | ParserConfigurationException e) { 

       e.printStackTrace(); 
      } 

     } 



} 

現在,當我提供 「//book」 作爲輸入,它給了我

[book: null] 
[book: null] 
[book: null] 
[book: null] 

"/bookstore/book[3]/author「會給

[author: null] 
[author: null] 
[author: null] 
[author: null] 
[author: null] 

回答

0

變化:

 Object result = xpathexp.evaluate(doc, XPathConstants.NODESET); 

TO

 Object result = xpathexp.evaluate(doc, XPathConstants.NODE); 
+0

它正常工作與以下XPath /書店/書[3] /作者[2] 輸出: [#text:每Bothner] 但是,使用XPath這樣 //書 輸出是: [#text: ] [標題:空] [#text: ] [作者:null] [#text: ] [同期:空] [#text: ] [price:null] [#text: ] –

+0

@Vishal_Kotecha你不能在一個循環中得到它。你必須添加更多的循環來獲取childNotes。 – Jens

+0

好的。萬分感謝!還有一件事,是否有任何這樣的程序的通用代碼,使我輸出給定的xpath(對於使用xereces)? 我對這件事很陌生。 –

0

我onl Y讀取您的帖子對角,但嘗試:

factory.setNamespaceAware(false); 
+0

它的工作不是:( 你能否詳細說明是什麼使輸出空? –

+0

XPath默認情況下(據我記得)是名稱空間感知。您不使用可能導致空值的名稱空間。 – nablex