我正在测试使用此代码获取我的 xml 值
static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
String name = parser.getValue(e, KEY_NAME); // name child value
String cost = parser.getValue(e, KEY_COST); // cost child value
String description = parser.getValue(e, KEY_DESC); // description child value
}
xml 的子元素是相同的,唯一的区别是我在 asp.net 10.0.2.2:57843/menus/json/getall上获取 xml结构 xml
**<ArrayOfitem>**
<item>
<cost>445</cost>
<description>sarap</description>
<id>1</id>
<name>pizza</name>
</item>
与 http://api.androidhive.info/pizza/?format=xml相比
**<menu>**
<item>
<id>1</id>
<name>Margherita</name>
<cost>155</cost>
<description>Single cheese topping</description>
</item>
我有一个 logcat 错误
07-08 04:10:10.329: E/Error:(1968): Unexpected token (position:TEXT {"ExceptionType"...@1:922 in java.io.StringReader@b5182a40)
07-08 04:10:10.329: E/AndroidRuntime(1968): FATAL EXCEPTION: main
07-08 04:10:10.329: E/AndroidRuntime(1968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException
07-08 04:10:10.329: E/AndroidRuntime(1968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException Caused by: java.lang.NullPointerException at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:43
line 43 NodeList nl = doc.getElementsByTagName(KEY_ITEM); and we got the same parent node of <item>
有人有时间解释或纠正我的问题吗?,谢谢。