我想从 XML 文件中获取数据:https ://www.vietcombank.com.vn/exchangerates/ExrateXML.aspx
错误是我可以获得最后一个元素 USD 并通过其他元素。请帮我
这是我的全部课程: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 a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
// create a SAXXMLHandler
SaxHandler saxHandler = new SaxHandler();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
// the process starts
xmlReader.parse(new InputSource(is));
// get the `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);
}
}
}