SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = null;
public Map<String, String> getUpdateInfo(String xml) {
xml = trope(xml);
try {
reader = factory.newSAXParser().getXMLReader();
} catch (SAXException e) {
e.printStackTrace();
return null;
} catch (ParserConfigurationException e) {
e.printStackTrace();
return null;
}
// 爲XMLReader設置內容處理器
GetUpdateInfoContentHandler c = new GetUpdateInfoContentHandler();
reader.setContentHandler(c);
try {
// 開始解析
reader.parse(new InputSource(new StringReader(xml)));
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (SAXException e) {
e.printStackTrace();
return null;
}
return c.getUpdateInfo();
}
}
-------------------------------------------------------------------------package seventh7.barcode.client.xml;
import java.util.HashMap;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class GetUpdateInfoContentHandler extends DefaultHandler {
String type = "";
String latestVer = "";
String apkDownloadPath = "";
boolean tag = false;
public void startElement(String namespaceURI, String localName,
String qName, Attributes attr) throws SAXException {
if(localName.equals("App") && !tag) {
type = attr.getValue("type");
latestVer = attr.getValue("latestVer");
apkDownloadPath = attr.getValue("apkDownloadPath");
if(type.equals("AirOrder"))
tag = true;
}
}
public Map<String, String> getUpdateInfo() {
Map<String, String> map = new HashMap<String, String>();
map.put("Type", type);
map.put("LatestVer", latestVer);
map.put("ApkDownloadPath", apkDownloadPath);
return map;
}
}