2013-05-19 30 views
0

我在印刷中存在的節點在給定的XML file.I使用該代碼,並將其編譯正確,但沒有顯示任何信息的屬性值的問題:一些錯誤打印XML節點屬性值

XPathFactory factory = XPathFactory.newInstance(); 
XPath xpath = factory.newXPath(); 
XPathExpression expr = xpath.compile("//rss/channel/yweather:location/@city"); 
Object result = expr.evaluate(doc, XPathConstants.STRING); 
System.out.println(result); 

和XML文件是:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> 
<channel> 
    <title>Yahoo! Weather - Sunnyvale, CA</title> 
    <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link> 
    <description>Yahoo! Weather for Sunnyvale, CA</description> 
    <language>en-us</language> 
    <lastBuildDate>Fri, 18 Dec 2009 9:38 am PST</lastBuildDate> 
    <ttl>60</ttl> 
    <yweather:location city="Sunnyvale" region="CA" country="United States"/> 
    <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/> 
</channel> 
</rss> 
+1

你註冊過'yweather'命名空間嗎? – choroba

+0

註冊它意味着什麼? –

+1

閱讀有關命名空間的內容,例如。在[Wikipedia](http://en.wikipedia.org/wiki/XML_Namespace)上查看['setNamenpaceContext(...)'](http://docs.oracle.com/javase/6/ docs/api/javax/xml/xpath/XPath.html#setNamespaceContext(javax.xml.namespace.NamespaceContext)並應用yahoo的wheather預測名稱空間。或者在每個元素(名稱空間通配符)前使用'*'你在最後一個問題中回答的評論。 –

回答

2

下面的代碼工作與在XPath命名空間參考。關鍵點是實現NamespaceContext並呼籲domFactory.setNamespaceAware(true) ...

import java.util.Iterator; 
import javax.xml.XMLConstants; 
import javax.xml.namespace.NamespaceContext; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathFactory; 
import org.w3c.dom.Document; 

public class Demo 
{ 
    public static void main(String[] args) 
    { 
     DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
     domFactory.setNamespaceAware(true); 
     try 
     { 
      DocumentBuilder builder = domFactory.newDocumentBuilder(); 
      Document dDoc = builder.parse("c:\\path\\to\\xml\\file.xml"); 
      XPath xPath = XPathFactory.newInstance().newXPath(); 
      xPath.setNamespaceContext(new UniversalNamespaceResolver(dDoc)); 
      String query = "//rss/channel/yweather:location/@city"; 
      XPathExpression expr = xPath.compile(query); 
      Object result = expr.evaluate(dDoc, XPathConstants.STRING); 
      System.out.println(result); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public static class UniversalNamespaceResolver implements NamespaceContext 
    { 
     private Document sourceDocument; 

     public UniversalNamespaceResolver(Document document) 
     { 
      sourceDocument = document; 
     } 

     public String getNamespaceURI(String prefix) 
     { 
      if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) 
       return sourceDocument.lookupNamespaceURI(null); 
      else 
       return sourceDocument.lookupNamespaceURI(prefix); 
     } 

     public String getPrefix(String namespaceURI) 
     { 
      return sourceDocument.lookupPrefix(namespaceURI); 
     } 

     public Iterator getPrefixes(String namespaceURI) 
     { 
      return null; 
     } 
    } 
} 

請確保在運行前更改文件路徑。

+0

謝謝,它工作 –

+0

不客氣:) – davmos