The packages and classes for this are different in Android. Use Android specific classes.
Kumar Bibek http://techdroid.kbeanie.com http://www.kbeanie.com On Thu, Jan 13, 2011 at 5:58 PM, dhatchina moorthy < [email protected]> wrote: > I try to parse the xml file in android. > > Before that i just implement in java, that code is working fine and i > parsed the xml. > > but i can't do the same in android, can anyone help me. > > My code in java, > > Code: > import org.w3c.dom.*; > import javax.xml.parsers.*; > import java.io.*; > import java.io.BufferedReader; > import java.io.IOException; > import java.io.InputStreamReader; > import java.net.HttpURLConnection; > import java.net.URL; > import java.lang.Byte; > import org.xml.sax.*; > import javax.xml.transform.*; > import javax.xml.transform.dom.DOMSource; > import javax.xml.transform.stream.StreamResult; > > public class GetRootNode{ > > public static final String WEATHER = "weather.txt"; > > public static void main(String[] args) { > > > try{ > > /* BufferedReader bf = new BufferedReader(new > InputStreamReader(System.in)); > > System.out.print("Enter xml file name: "); > String str = bf.readLine(); > > File file = new File(str); > if (file.exists()){ */ > > > HttpURLConnection con; > URL url = new URL("http://www.google.com/ig/api?alt=&weather=" + > "chennai"); > con = (HttpURLConnection) url.openConnection(); > con.setReadTimeout(10000 /* milliseconds */); > con.setConnectTimeout(15000 /* milliseconds */); > con.setRequestMethod("GET" ); > con.connect(); > > // Receiving request is converted into string > BufferedReader reader = new BufferedReader(new > InputStreamReader(con.getInputStream(), "UTF-8" )); > String payload = reader.readLine(); > //System.out.println(payload); > > PrintWriter out = new PrintWriter(new FileWriter(WEATHER)); > out.print(payload); > out.close(); > > File str = new File(WEATHER); > > if (str.exists()){ > > DocumentBuilderFactory fact = > DocumentBuilderFactory.newInstance(); > DocumentBuilder builder = fact.newDocumentBuilder(); > Document doc = builder.parse(str); > Node node = doc.getDocumentElement(); > String root = node.getNodeName(); > System.out.println("Root Node: " + root); > > NodeList list = doc.getElementsByTagName("*"); > > System.out.println("XML Elements: "); > > Element element; > > for (int i=3; i<=9; i++) { > // Get element > element = (Element)list.item(i); > System.out.println( element.getNodeName() + " : " > +element.getAttribute("data")) ; > //System.out.println(element.getAttribute("data")); > > } > > System.out.println("\n"); > > for (int i=10; i<=16; i++) { > // Get element > element = (Element)list.item(i); > System.out.println( element.getNodeName() + " : " > +element.getAttribute("data")) ; > //System.out.println(element.getAttribute("data")); > > } > > > /* for (int i=0; i<list.getLength(); i++) { > // Get element > element = (Element)list.item(i); > System.out.println( i + "-" + element.getNodeName() > + ":" > +element.getAttribute("data")) ; > //System.out.println(element.getAttribute("data")); > > } */ > > } > > else{ > System.out.println("File not found!"); > > } > } > catch(Exception e){} > } > > } > > > Output: > > Root Node: xml_api_reply > XML Elements: > city : Chennai, Tamil Nadu > postal_code : chennai > latitude_e6 : > longitude_e6 : > forecast_date : 2011-01-13 > current_date_time : 2011-01-13 11:40:00 +0000 > unit_system : US > > > current_conditions : > condition : Mostly Cloudy > temp_f : 79 > temp_c : 26 > humidity : Humidity: 65% > icon : /ig/images/weather/mostly_cloudy.gif > wind_condition : Wind: N at 8 mph > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected]<android-developers%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

