大家好,我对xmlPullParser有问题。我正在努力遵循谷歌开发者教程中的一点修改。我正在从原始资源文件中获取xml文件。该文件名为employee.xml
它看起来像下面。
<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee>
<id>1</id>
<firstName>John</firstName>
<lastName>Doe</lastName>
<title>CEO</title>
<city>San Francisco, CA</city>
<managerId>0</managerId>
<department>Corporate</department>
<officePhone>123-456-0001</officePhone>
<mobilePhone>987-654-1234</mobilePhone>
<email>John@mail.com</email>
<picture>placeholder.jpg</picture>
</employee>
<employee>
<id>2</id>
<firstName>Barack</firstName>
<lastName>Obama</lastName>
<title>President</title>
<city>Washington DC</city>
<department>Corporate</department>
<managerId>0</managerId>
<officePhone>123-456-0002</officePhone>
<mobilePhone>781-000-0002</mobilePhone>
<email>Barack@mail.com</email>
<picture>barack_obama.jpg</picture>
</employee>
<employee>
<id>3</id>
<firstName>Joe</firstName>
<lastName>Biden</lastName>
<title>VP</title>
<city>Washington DC</city>
<managerId>2</managerId>
<department>Corporate</department>
<officePhone>123-456-0003</officePhone>
<mobilePhone>987-654-1234</mobilePhone>
<email>Joe@mail.com</email>
<picture>joe_biden.jpg</picture>
</employee>
<employee>
<id>4</id>
<firstName>Hillary</firstName>
<lastName>Clinton</lastName>
<title>Secretary of State</title>
<city>Washington DC</city>
<managerId>2</managerId>
<department>Corporate</department>
<officePhone>123-456-0004</officePhone>
<mobilePhone>987-654-1234</mobilePhone>
<email>Hillary@mail.com</email>
<picture>hillary_clinton.jpg</picture>
</employee>
</employees>
但当我试图在我的代码中插入它时,我得到了异常。下面是我解析这个xml文件的代码,我从主活动调用它
public List<Employee> parse(Context cont) throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
InputStream stream = cont.getResources().openRawResource(R.raw.employee);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(stream, null);
parser.next();
return readFeed(parser);
}
private List<Employee> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException
{
employeelist = new ArrayList<>();
Log.d("current tag: ", parser.getName());
Log.d("TAG", "The event type is: " + parser.getEventType());
parser.require(XmlPullParser.START_TAG, parser.getName().toString(), "employees");
Log.d("current tag : ", parser.getName());
while(parser.next() != XmlPullParser.END_TAG)
{
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if(name.equals("employee"))
{
employeelist.add(readEntry(parser));
}
else
{
//skip unwanted entries!!
skip(parser);
}
}
return employeelist;
}
当我在readFeed()方法中调用"parser.require()"时,会抛出xmlpullparserexception。我不知道为什么,因为它似乎找到了正确的"员工"标签!!就像我在调用readFeed之前调用parser.next()一样!!这是堆垛机。
11-18 08:37:51.476 4660-4676/? D/current tag:: employees
11-18 08:37:51.476 4660-4676/? D/TAG: The event type is: 2
11-18 08:37:51.477 4660-4676/? W/System.err: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {employees}employees (position:START_TAG <employees>@2:12 in java.io.InputStreamReader@41f7ff40)
11-18 08:37:51.477 4660-4676/? W/System.err: at org.kxml2.io.KXmlParser.require(KXmlParser.java:2056)
11-18 08:37:51.477 4660-4676/? W/System.err: at com.example.eoin_a.xmlparserexample.EmployeeXmlParser.readFeed(EmployeeXmlParser.java:57)
11-18 08:37:51.477 4660-4676/? W/System.err: at com.example.eoin_a.xmlparserexample.EmployeeXmlParser.parse(EmployeeXmlParser.java:48)
11-18 08:37:51.477 4660-4676/? W/System.err: at com.example.eoin_a.xmlparserexample.MainActivity$1.run(MainActivity.java:55)
11-18 08:37:51.477 4660-4676/? W/System.err: at java.lang.Thread.run(Thread.java:841)
11-18 08:37:51.479 4660-4660/? D/AndroidRuntime: Shutting down VM
11-18 08:37:51.479 4660-4660/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41670d40)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: FATAL EXCEPTION: main
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: Process: com.example.eoin_a.xmlparserexample, PID: 4660
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eoin_a.xmlparserexample/com.example.eoin_a.xmlparserexample.MainActivity}: java.lang.NullPointerException
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5086)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-18 08:37:51.481 4660-4660/? E/AndroidRuntime: at com.example.eoin_a.xmlparserexample.MainActivity.onCreate(MainActivity.java:38)
我还包括了第一条调试日志消息,因为我认为它看起来很不寻常。它输出2个冒号而不是1。我不知道为什么!!如果有人能帮我理解这个异常,那就太好了,因为目前我还不理解下面的异常或是什么原因造成的。谢谢!
org.xmlpull.v1.XmlPullParserException: expected: START_TAG {employees}employees (position:START_TAG <employees>@2:12 in java.io.InputStreamReader@41f7ff40)
这里有一个指向parser.request()文档的链接。
public abstract void require (int type, String namespace, String name)
中间一个是我认为造成你问题的原因。parser.require
正在寻找正确的标签类型、正确的命名空间和正确的标签名称;如果其中一个不匹配,它将抛出一个异常。
在您的例子中,对于中间的参数,您使用getName来获取当前标记("employees")的名称,并询问parser.request()来检查它是否与命名空间匹配。命名空间与标记名不同,因此会引发异常。
当使用parser.require()
时,您不需要自己使用getName
,只需要传入您期望的名称即可。
根据文档"null将匹配任何命名空间和任何名称"。如果您正在做的是检查是否有<employees>
开始标记,那么您可以使用null
作为名称空间参数来忽略名称空间部分:
parser.require(XmlPullParser.START_TAG, null, "employees");
这将检查它是否是一个开始标记,并且标记名称是否为"employees"。