对于config.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="server_api">http://www.phpwind.net/</string>
<string name="app_name">phpwind</string>
<string name="app_label">phpwind</string>
</resources>
1. DOM方式
public void getByDOM() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(getResources().getAssets().open("xmlfile/config.xml"));
Element element = document.getDocumentElement();
NodeList nodes = element.getElementsByTagName("string");
for (int i = 0; i < nodes.getLength(); i++) {
Element node = (Element) nodes.item(i);
Log.i(TAG, "name:" + node.getAttribute("name") + ", value:" + node.getFirstChild().getNodeValue());
// node.getTextContent();
}
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
}
2. Pull方式
public void getByPull() {
XmlPullParser xml = null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
xml = factory.newPullParser();
xml.setInput(getAssets().open("xmlfile/config.xml"), "utf-8");
int tagType = xml.next();
while (tagType != XmlPullParser.END_DOCUMENT) {
if (tagType == XmlPullParser.START_TAG && xml.getName().equals("string")) {
Log.i(TAG, xml.getAttributeValue("", "name"));
xml.next();
Log.i(TAG, "----" + xml.getText());
}
tagType = xml.next();
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}