2012-07-03 342 views
0

我要打印的属性值,下面的XML文件:为什么我的属性值不能打印?

<data/> 
     <request/> 
       <type>City</type> 
       <query>Jaipur, India</query> 
      </request> 
     <current_condition/> 
       <observation_time>06:37 AM</observation_time> 
       <temp_C>40</temp_C> 
       <temp_F>104</temp_F> 
       <weatherCode>113</weatherCode> 
       <weatherIconUrl/> 
       <weatherDesc/> 
       <windspeedMiles>8</windspeedMiles> 
       <windspeedKmph>13</windspeedKmph> 
       <winddirDegree>280</winddirDegree> 
       <winddir16Point>W</winddir16Point> 
       <precipMM>0.0</precipMM> 
       <humidity>34</humidity> 
       <visibility>4</visibility> 
       <pressure>997</pressure> 
       <cloudcover>25</cloudcover> 
      </current_condition> 
     <weather/> 
       <date>2012-07-03</date> 
       <tempMaxC>46</tempMaxC> 
       <tempMaxF>115</tempMaxF> 
       <tempMinC>36</tempMinC> 
       <tempMinF>97</tempMinF> 
       <windspeedMiles>6</windspeedMiles> 
       <windspeedKmph>9</windspeedKmph> 
       <winddirection>N</winddirection> 
       <winddir16Point>N</winddir16Point> 
       <winddirDegree>7</winddirDegree> 
       <weatherCode>113</weatherCode> 
       <weatherIconUrl/> 
       <weatherDesc/> 
       <precipMM>0.0</precipMM> 
      </weather> 
     <weather/> 
       <date>2012-07-04</date> 
       <tempMaxC>46</tempMaxC> 
       <tempMaxF>114</tempMaxF> 
       <tempMinC>34</tempMinC> 
       <tempMinF>94</tempMinF> 
       <windspeedMiles>12</windspeedMiles> 
       <windspeedKmph>20</windspeedKmph> 
       <winddirection>NW</winddirection> 
       <winddir16Point>NW</winddir16Point> 
       <winddirDegree>319</winddirDegree> 
       <weatherCode>113</weatherCode> 
       <weatherIconUrl/> 
       <weatherDesc/> 
       <precipMM>0.0</precipMM> 
      </weather> 
    </data> 

下面是我使用的打印属性值的Java代码:

public class WorldWeatherOnline { 
     public static void main (String[] args) throws IOException, ParserConfigurationException, SAXException, TransformerException { 
      URL url = new URL("http://free.worldweatheronline.com/feed/weather.ashx?q=jaipur,india&format=xml&num_of_days=2&key=c5774216f9120304120207"); 
      URLConnection conn = url.openConnection(); 

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder builder = factory.newDocumentBuilder(); 
      Document doc = builder.parse(conn.getInputStream()); 
      printElementAttributes(doc); 
    } 
static void printElementAttributes(Document doc) 
    { 
     NodeList nl = doc.getElementsByTagName("*"); 
     Element e; 
     Node n; 
     NamedNodeMap nnm; 

     String attrname; 
     String attrval; 
     int i, len; 

     len = nl.getLength(); 
     for (int j=0; j < len; j++) 
     { 
      e = (Element)nl.item(j); 
      System.out.println(e.getTagName() + ":"); 
      System.out.println("check-1"); 

      nnm = e.getAttributes(); 
      if (nnm != null) 
      { 
      for (i=0; i<nnm.getLength(); i++) 
      { 
       System.out.println("check"); 
       n = nnm.item(i); 
       attrname = n.getNodeName(); 
       attrval = n.getNodeValue(); 
       System.out.print(" " + attrname + " = " + attrval); 
      } 
      } 
      System.out.println(); 
     } 
    } 

基本上我的NamedNodeMap NNM为空,所以它不会进入循环。我用这些进口:

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NamedNodeMap; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

回答

2

你的XML不任何属性。它只是包含文本内容的元素。与属性的元素看起来是这样的:

<element attributeName="attribute value" /> 

如果你想使用当前XML,你需要获得元素的文本值而不是,想必存储那些反对该元素的名称。

+0

非常感谢。得到它了。在节点上使用getTextContent()方法。 – rishiag