分析xml android时会出现空指针异常
我正在分析以下XML:分析xml android时会出现空指针异常,android,xml,xpath,android-xml,android-parser,Android,Xml,Xpath,Android Xml,Android Parser,我正在分析以下XML: <?xml version="1.0"?> <Root> <ResponseCode>1</ResponseCode> <ResponseMessage>Login Successfull</ResponseMessage> <ResultSet> <User> <id>3</id>
<?xml version="1.0"?>
<Root>
<ResponseCode>1</ResponseCode>
<ResponseMessage>Login Successfull</ResponseMessage>
<ResultSet>
<User>
<id>3</id>
<username>hassan</username>
<email>hassan</email>
<password>abcd</password>
<profileimagepath>c:/hahaha/hahaha</profileimagepath>
<createdOn>2013-03-23 12:45:51</createdOn>
<status>1</status>
</User>
</ResultSet>
</Root>
1.
登录成功
3.
哈桑
哈桑
abcd
c:/哈哈哈/哈哈哈
2013-03-23 12:45:51
1.
我用来解析XML的代码是:
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlResult));
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
try {
NodeList list = (NodeList) xPath.evaluate("/Root", is,XPathConstants.NODESET);
for(int i=0; i<list.getLength(); i++){
Node node = list.item(i);
Element element = (Element) node;
Log.d("VALUE OF NODE", element.getNodeValue());
}
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputSource is=new InputSource();
is.setCharacterStream(新的StringReader(xmlResult));
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
试一试{
NodeList list=(NodeList)xPath.evaluate(“/Root”,即XPathConstants.NODESET);
对于(int i=0;i您从Log.d()
内部得到一个异常,因为元素.getNodeValue()
正在返回null
不能将<代码> null <代码>作为第二个参数,输入到Log.D.())/>代码中。请考虑将相应的行更改为:
Log.d("xml", "VALUE OF NODE: " + element.getNodeValue());
与其专注于具体问题,不如尝试理解解释堆栈跟踪。堆栈跟踪指向确切的问题:
E/AndroidRuntime(1097):位于android.util.Log.d(Log.java:138)
E/AndroidRuntime(1097):位于com.teamgreen.greenit.LoginActivity$1$1.run(LoginActivity.java:87)
请在这里添加logcat。这解决了异常,但我无法获取内部数据,我该怎么做?您找到问题的解决方案了吗?我们如何检查标记是否包含空值?我不确定我是否完全理解您的问题,但您可以使用元素检查空值。getNodeValue()==null
Log.d("xml", "VALUE OF NODE: " + element.getNodeValue());