为什么我的属性值没有打印?

时间:2012-07-03 09:00:40

标签: java xml xml-parsing

我正在尝试打印以下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为null,所以它不会进入循环内部。我使用过这些进口商品:

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;

1 个答案:

答案 0 :(得分:2)

您的XML 没有任何属性。它只包含带有文本内容的元素。具有属性的元素如下所示:

<element attributeName="attribute value" />

如果您想使用当前的XML,则需要获取元素的文本值,并且可能会根据元素名称存储这些值。

相关问题