How to parse data from localhost xml file in android -
hi iam trying parse xml file hosted in localhost server. on executing did'nt data. please me if knows.
my code:
package com.example.androidtablayout; import java.io.ioexception; import java.io.inputstream; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.w3c.dom.document; import org.w3c.dom.nodelist; import android.app.activity; import android.os.asynctask; import android.os.bundle; public class photosactivity extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.photos_layout); try { new photosactivity().start(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } private void start() throws exception { url url = new url("http://localhost/mini.xml"); urlconnection connection = url.openconnection(); document doc = parsexml(connection.getinputstream()); nodelist descnodes = doc.getelementsbytagname("category"); nodelist descnodes1 = doc.getelementsbytagname("name"); nodelist descnodes2 = doc.getelementsbytagname("price"); nodelist descnodes3 = doc.getelementsbytagname("icon"); for(int i=0; i<descnodes1.getlength();i++) { system.out.println(descnodes.item(i).gettextcontent()); system.out.println(descnodes1.item(i).gettextcontent()); system.out.println(descnodes2.item(i).gettextcontent()); system.out.println(descnodes3.item(i).gettextcontent()); } } private document parsexml(inputstream stream) throws exception { documentbuilderfactory objdocumentbuilderfactory = null; documentbuilder objdocumentbuilder = null; document doc = null; try { objdocumentbuilderfactory = documentbuilderfactory.newinstance(); objdocumentbuilder = objdocumentbuilderfactory.newdocumentbuilder(); doc = objdocumentbuilder.parse(stream); } catch(exception ex) { throw ex; } return doc; } }
my localhost xml:
<truck> <truckdata> <id>1</id> <category>small trucks</category> <name>mercedes-benz</name> <price>16,000.00</price> <icon>benz</icon> </truckdata> <truckdata> <id>2</id> <category>box trucks</category> <name>volkswagen lt 46</name> <price>26,000.00</price> <icon>volks</icon> </truckdata> <truckdata> <id>3</id> <category>pickup trucks</category> <name>bmw m3</name> <price>38,000.00</price> <icon>bmw</icon> </truckdata> <truckdata> <id>4</id> <category>row truck</category> <name>chevrolet silverado</name> <price>34,000.00</price> <icon>chev</icon> </truckdata> </truck>
Comments
Post a Comment