簡體   English   中英

打印XML節點屬性值有問題

[英]Something wrong in printing XML node attribute value

我在打印給定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>

以下代碼適用於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;
        }
    }   
}

請務必在運行前更改文件路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM