Web サイト「sjc.com.vn」からファイル xml を読みたいのですが、うまくいきません。ここに私のソースコードがあります。
enter code here
public class OrderXMLHandler extends DefaultHandler {
boolean currentElement = false;
String currentValue = "";
String ratelist;
String updated;
ProductInfo productInfo;
ArrayList<ProductInfo> cartList;
public String getRatelist() {
return ratelist;
}
public void setRatelist(String ratelist) {
this.ratelist = ratelist;
}
public String getUpdated() {
return updated;
}
public void setUpdated(String updated) {
this.updated = updated;
}
public ArrayList<ProductInfo> getCartList() {
return cartList;
}
public void setCartList(ArrayList<ProductInfo> cartList) {
this.cartList = cartList;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if(qName.equals("root")){
cartList = new ArrayList<ProductInfo>();
} else if(qName.equals("city"))
productInfo = new ProductInfo();
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
if(localName.equalsIgnoreCase("city"))
productInfo.setName(currentValue.trim());
else if (localName.equalsIgnoreCase("buy"))
productInfo.setBuy(currentValue.trim());
else if (localName.equalsIgnoreCase("sell"))
productInfo.setSell(currentValue.trim());
else if (localName.equalsIgnoreCase("type"))
productInfo.setType(currentValue.trim());
else if (localName.equalsIgnoreCase("city"))
cartList.add(productInfo);
currentValue = "";
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = currentValue + new String(ch, start, length);
}
}
}
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Thực hiện hàm phân tích XML
parseXML();
}
//Hàm phân tích XML
private void parseXML(){
try {
URL url=new URL("http://www.sjc.com.vn/xml/tygiavang.xml");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
OrderXMLHandler myXMLHandler = new OrderXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(url.openStream()));
TextView tv = new TextView(this);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);
//In chi tiết sản phẩm ra giao diện ứng dụng
ArrayList<ProductInfo> cartList = myXMLHandler.getCartList();
for (ProductInfo productInfo : cartList) {
tv = new TextView(this);
tv.setText("Ten Thanh Pho: " + productInfo.getName());
ll.addView(tv);
tv = new TextView(this);
tv.setText("Buy : " + productInfo.getBuy());
ll.addView(tv);
tv = new TextView(this);
tv.setText("sell : " + productInfo.getSell());
ll.addView(tv);
tv = new TextView(this);
tv.setText("type : " + productInfo.getType());
ll.addView(tv);
tv = new TextView(this);
tv.setText("---");
ll.addView(tv);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
パブリック クラス ProductInfo {
String type="";
String sell="";
String buy="";
String name ="";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSell() {
return sell;
}
public void setSell(String sell) {
this.sell = sell;
}
public String getBuy() {
return buy;
}
public void setBuy(String buy) {
this.buy = buy;
}
}