i want data xml file :https://www.vietcombank.com.vn/exchangerates/exratexml.aspx
the error can last element usd , pass others. please me
here class: exrate.java
public class exrate { string currencycode; string currencyname; string buy; string transfer; string sell; public exrate(){} public string getcurrencycode() { return currencycode; } public void setcurrencycode(string currencycode) { this.currencycode = currencycode; } // public string getcurrencyname(){ return currencyname; } public void setcurrencyname(string currencyname){ this.currencyname=currencyname; } // public string getbuy(){ return buy; } public void setbuy(string buy ){ this.buy=buy; } // public string gettransfer(){ return transfer; } public void settransfer(string transfer ){ this.transfer=transfer; } // public string getsell(){ return sell; } public void setsell(string sell ){ this.sell=sell; } }
exratelist.java
public class exratelist { string datetime; exrate exrate; public exratelist(){} public string getdatetime(){ return datetime; } public void setdatetime(string datetime){ this.datetime=datetime; } // public exrate getexrate(){ return exrate; } public void setexrate(exrate exrate){ this.exrate=exrate; } }
saxhandler.java
public class saxhandler extends defaulthandler { string reading; arraylist<exratelist> exratelists; exratelist exratelist; exrate exrate; public saxhandler() { exratelists = new arraylist<exratelist>(); } @override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { if (qname.equalsignorecase("exratelist")){ exratelists=new arraylist<>(); exratelist=new exratelist(); } if (qname.equalsignorecase("exrate")){ exrate=new exrate(); exrate.setcurrencycode(attributes.getvalue("currencycode")); exrate.setcurrencyname(attributes.getvalue("currencyname")); exrate.setbuy(attributes.getvalue("buy")); exrate.settransfer(attributes.getvalue("transfer")); exrate.setsell(attributes.getvalue("sell")); } } @override public void endelement(string uri, string localname, string qname) throws saxexception { if (qname.equalsignorecase("exratelist")){ if (exratelists!=null){ exratelists.add(exratelist); exratelist=null; } } if (qname.equalsignorecase("datetime")){ exratelist.setdatetime(reading); } else if (qname.equalsignorecase("exrate")){ exratelist.setexrate(exrate); } } @override public void characters(char[] ch, int start, int length) throws saxexception { reading=new string(ch, start, length); } public list<exratelist> getexratelists() { return exratelists; } }
mysaxparser.java
public class mysaxparser { public list<exratelist> xmlparse(inputstream is) { list<exratelist> exratelists = null; try { // create xmlreader saxparser xmlreader xmlreader = saxparserfactory.newinstance().newsaxparser().getxmlreader(); // create saxxmlhandler saxhandler saxhandler = new saxhandler(); // store handler in xmlreader xmlreader.setcontenthandler(saxhandler); // process starts xmlreader.parse(new inputsource(is)); // `employee list` exratelists =saxhandler.getexratelists(); } catch (exception ex) { log.d("xml", "saxxmlparser: parse() failed"); } // return employee list return exratelists; } } mainactivity.java public class mainactivity extends appcompatactivity { textview ngay, chuoi; string duongdan = "https://www.vietcombank.com.vn/exchangerates/exratexml.aspx"; list<exratelist> exratelists = new arraylist<exratelist>(); string datetime; string macode=""; string tencode=""; string mua=""; inputstream is; url url; urlconnection connection; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ngay = (textview) findviewbyid(r.id.textview); chuoi = (textview) findviewbyid(r.id.textview2); task a=new task(); a.execute(); } class task extends asynctask<void,void,void> { @override protected void doinbackground(void... params) { try { url=new url(duongdan); connection=url.openconnection(); is=connection.getinputstream(); mysaxparser mysaxparser=new mysaxparser(); exratelists=mysaxparser.xmlparse(is); // datetime=exratelists.get(0).getdatetime(); for(int i=0;i<exratelists.size();i++) { macode+=exratelists.get(i).getexrate().getcurrencycode(); tencode+= exratelists.get(i).getexrate().getcurrencyname()+"\n"; mua+= exratelists.get(i).getexrate().getbuy(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); ngay.settext(datetime); chuoi.settext(macode+" "+tencode+" "+mua); } } }
i have used simple framework show example how can read xml tag.
create 2 classes create object act model classes.
import org.simpleframework.xml.attribute; import org.simpleframework.xml.root; /** * created pankaj nimgade on 29-01-2016. */ @root (name = "exrate", strict = false) public class exrate { @attribute(required = false) private string currencycode; @attribute(required = false) private string currencyname; @attribute(required = false) private string buy; @attribute(required = false) private string transfer; @attribute(required = false) private string sell; public exrate() { } public exrate(string currencycode, string currencyname, string buy, string transfer, string sell) { currencycode = currencycode; currencyname = currencyname; buy = buy; transfer = transfer; sell = sell; } public string getcurrencycode() { return currencycode; } public string getcurrencyname() { return currencyname; } public string getbuy() { return buy; } public string gettransfer() { return transfer; } public string getsell() { return sell; } }
exratelist
import org.simpleframework.xml.element; import org.simpleframework.xml.elementlist; import org.simpleframework.xml.root; import java.util.list; /** * created pankaj nimgade on 29-01-2016. */ @root (name = "exratelist", strict = false) public class exratelist { @element(required = false) private string datetime; @elementlist (inline = true,required= false) private list<exrate> exrate; @element(required = false) private string source; public exratelist() { } public exratelist(string datetime, string source) { datetime = datetime; source = source; } public string getdatetime() { return datetime; } public list<exrate> getlist() { return exrate; } public string getsource() { return source; } }
now can test code
public class readxml { public static void main(string[] args) { try { path paths = paths.get(""); string project_path = paths.toabsolutepath().tostring(); system.out.println("" + project_path); file file = new file(project_path + "\\excel_files\\some.xml"); serializer serializer = new persister(); exratelist example = serializer.read(exratelist.class, file); system.out.println("" + example.getdatetime()); (exrate exrate:example.getlist()) { system.out.println(""+exrate.getcurrencyname()+"\t "+exrate.getcurrencycode()+"\t "+exrate.gettransfer()+"\t "+exrate.getbuy()+"\t "+exrate.getsell()); } system.out.println("" + example.getsource()); } catch (exception e) { e.printstacktrace(); } } }
outout
1/29/2016 4:11:40 pm aust.dollar aud 15731.66 15637.27 canadian dollar cad 15768.98 15627.06 swiss france chf 21700.14 21548.24 danish krone dkk 3191.26 0 euro eur 24068.25 23996.05 british pound gbp 31737.38 31515.22 hongkong dollar hkd 2831.9 2812.08 indian rupee inr 320.61 0 japanese yen jpy 182.95 181.12 south korean won krw 18.48 0 kuwaiti dinar kwd 72009.45 0 malaysian ringgit myr 5340.84 0 norwegian kroner nok 2536.44 0 russian ruble rub 267.7 0 saudi rial sar 5745.75 0 swedish krona sek 2573.7 0 singapore dollar sgd 15520.44 15411.8 thai baht thb 609.21 609.21 dollar usd 22165 22165 joint stock commercial bank foreign trade of vietnam - vietcombank
Comments
Post a Comment