我正在使用 SAX XML Parser 从运行良好的 XML 文件中获取值。这是我主要活动中的方法:
public void parseXML() {
String parsedData = "";
try {
Log.w("AndroidParseXMLActivity", "Start");
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLContentHandler myXMLHandler = new XMLContentHandler();
xr.setContentHandler(myXMLHandler);
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("calimero.xml");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
xr.parse(new InputSource(inputStream));
ArrayList<Datapoint> itemsList = myXMLHandler.getItemsList();
int datapoint_size = itemsList.size();
String xml_names[] = new String [datapoint_size];
String xml_states[] = new String [datapoint_size];
String xml_dptIDs[] = new String [datapoint_size];
String xml_groupadresses[] = new String [datapoint_size];
String xml_mainNumbers[] = new String [datapoint_size];
for(int i=0;i<itemsList.size();i++){
Datapoint item = itemsList.get(i);
parsedData = parsedData + "----->\n";
parsedData = parsedData + "Name: " + item.getName() + "\n";
xml_names[i] = item.getName();
parsedData = parsedData + "stateBased: " + item.getStateBased() + "\n";
xml_states[i] = item.getStateBased();
parsedData = parsedData + "mainNumber: " + item.getMainNumber() + "\n";
xml_mainNumbers[i] = item.getMainNumber();
parsedData = parsedData + "dptID: "+ item.getDptID() + "\n";
xml_dptIDs[i] = item.getDptID();
parsedData = parsedData + "Groupadress: "+ item.getGroupadress() + "\n";
xml_groupadresses[i] = item.getGroupadress();
}
Log.w("AndroidParseXMLActivity", "Done");
}
catch (Exception e) {
Log.w("AndroidParseXMLActivity",e );
}
Log.w("AndroidParseXMLActivity", names[0]);
Log.w("AndroidParseXMLActivity", mainNumbers[2]);
Log.w("AndroidParseXMLActivity", dptIDs[3]);
//xmlOutput.setText(parsedData);
}
我想在我的方法之外使用名称、mainNumber 等值。但我不明白......我知道这是java基础......请帮助我,谢谢!