java - How to make a program that will download xml data? -
i need make program download xml data website using java program not javascript , put android app.
can me this?
btw next step parse it.
you can use method of read , write !
here code write xml file:
final string xmlfile="userdata"; string username="username"; string password="password"; try { fileoutputstream fileos= getapplicationcontext().openfileoutput(xmlfile, context.mode_private); xmlserializer xmlserializer = xml.newserializer(); stringwriter writer = new stringwriter(); xmlserializer.setoutput(writer); xmlserializer.startdocument("utf-8",true); xmlserializer.starttag(null, "userdata"); xmlserializer.starttag(null, "username"); xmlserializer.text(username_string_here); xmlserializer.endtag(null,"username"); xmlserializer.starttag(null,"password"); xmlserializer.text(password_string); xmlserializer.endtag(null, "password"); xmlserializer.endtag(null, "userdata"); xmlserializer.enddocument(); xmlserializer.flush(); string datawrite=writer.tostring(); fileos.write(datawrite.getbytes()); fileos.close(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
read data xml file:
final string xmlfile="userdata"; arraylist<string> userdata = new arraylist<string>(); try { fis = getapplicationcontext().openfileinput(xmlfile); isr = new inputstreamreader(fis); inputbuffer = new char[fis.available()]; isr.read(inputbuffer); data = new string(inputbuffer); isr.close(); fis.close(); } catch (filenotfoundexception e3) { // todo auto-generated catch block e3.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } xmlpullparserfactory factory = null; try { factory = xmlpullparserfactory.newinstance(); } catch (xmlpullparserexception e2) { // todo auto-generated catch block e2.printstacktrace(); } factory.setnamespaceaware(true); xmlpullparser xpp = null; try { xpp = factory.newpullparser(); } catch (xmlpullparserexception e2) { // todo auto-generated catch block e2.printstacktrace(); } try{ xpp.setinput( new stringreader (data) ); } catch (xmlpullparserexception e1) { // todo auto-generated catch block e1.printstacktrace(); } int eventtype = 0; try{ eventtype = xpp.geteventtype(); } catch (xmlpullparserexception e1) { // todo auto-generated catch block e1.printstacktrace(); } while (eventtype != xmlpullparser.end_document){ if(eventtype == xmlpullparser.start_document) { system.out.println("start document"); }else if(eventtype == xmlpullparser.start_tag) { system.out.println("start tag "+xpp.getname()); }else if(eventtype == xmlpullparser.end_tag) { system.out.println("end tag "+xpp.getname()); }else if(eventtype == xmlpullparser.text) { userdata.add(xpp.gettext()); } try{ eventtype = xpp.next(); }catch (xmlpullparserexception e) { // todo auto-generated catch block e.printstacktrace(); }catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } string username=userdata.get(0); string password=userdata.get(1);
or can save xml sd card
url url = new url("some url"); //create new connection httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); //set things on connection urlconnection.setrequestmethod("get"); urlconnection.setdooutput(true); //and connect! urlconnection.connect(); //set path want save file //in case, going save on root directory of //sd card. file sdcardroot = new file("/sdcard/"+"some folder name/"); //create new file, specifying path, , filename //which want save file as. file file = new file(sdcardroot,"some file name"); //this used write downloaded data file created fileoutputstream fileoutput = new fileoutputstream(file); //this used in reading data internet inputstream inputstream = urlconnection.getinputstream(); //this total size of file int totalsize = urlconnection.getcontentlength(); //variable store total downloaded bytes int downloadedsize = 0; //create buffer... byte[] buffer = new byte[1024]; int bufferlength = 0; //used store temporary size of buffer //now, read through input buffer , write contents file while ( (bufferlength = inputstream.read(buffer)) > 0 ) { //add data in buffer file in file output stream (the file on sd card fileoutput.write(buffer, 0, bufferlength); //add size know how downloaded downloadedsize += bufferlength; int progress=(int)(downloadedsize*100/totalsize); //this report prgress, maybe //updateprogress(downloadedsize, totalsize); } //close output stream when done fileoutput.close();
hope you..!
Comments
Post a Comment