我想解析xml文件如下:
<?xml version='1.0' encoding='UTF-8'?>
<rsp status='ok'>
<status_id>1111</status_id>
<user_id>TwitUsername</user_id>
<media_id>ZZ83F</media_id>
</rsp>
我使用 DOM 来解析文件 xml,如下所示:
public String getStatus()
{
String status="";
try {
InputStream is=this.getResources().openRawResource(R.raw.json);
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
Element root = xmlDoc.getDocumentElement();
NodeList rsp = root.getElementsByTagName("rsp");
for(int i=0;i<rsp.getLength();i++)
{
Node curNode = rsp.item(i);
// this tag is <study>, get `id` attribute first
status=String.valueOf(((Attr)curNode.getAttributes().item(0)).getValue());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return status;
}
但是getStatus方法返回 null。