Why my attribute values are not printed?

badvnn

New Member
I am trying to print the attributes values for following xml file:\[code\]<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>\[/code\]Following is the java code which I am using to print the attribute values:\[code\] 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(); } }\[/code\]Basically my NamedNodeMap nnm is null, so it isn't going inside the loop. I have used these imports:\[code\]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;\[/code\]
 
Back
Top