我正在尝试xml
从远程服务器解析。
我的 3 个java
程序的代码如下:
1. BillScreen.java:
package cc3012n.kalrashid;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class BillScreen extends Activity implements OnClickListener {
String baseURL = "http://www.connectingtomorrow.co.uk/xmlFiles/accounts.xml";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById (R.id.bGo);
tv = (TextView) findViewById (R.id.tvXML);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
URL website = new URL(baseURL);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff doingWork = new HandlingXMLStuff();
xr.setContentHandler(doingWork);
xr.parse(new InputSource(website.openStream()));
String information = doingWork.getInformation();
tv.setText(information);
} catch(Exception e){
tv.setText("error");
}
}
}
2. 处理 XML 的东西:
package cc3012n.kalrashid;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class HandlingXMLStuff extends DefaultHandler {
XMLDataCollected info = new XMLDataCollected();
public String getInformation() {
return info.dataToString();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("customer_id")) {
String customer = attributes.getValue("data");
info.setCustomer(customer);
} else if (localName.equals("table_id")) {
String table = attributes.getValue("data");
info.setTable(table);
}
}
}
3. XMLDataCollected.java:
public class XMLDataCollected {
String customer = null;
String table = null;
public void setCustomer (String ac){
customer = ac;
}
public void setTable(String tbl){
table = tbl;
}
public String dataToString(){
return customer + " In table Number " +table;
}
}
但它返回一个错误..所以它无法读取xml
文件..你能告诉我我做错了什么吗?
提前谢谢了。