在 android xmlPullParserException 中使用 xmlPullParser 解析本地 xml 文件时出现问题
troubles parsing local xml file with xmlPullParser in android xmlPullParserException
大家好,我遇到了 xmlPullParser 的问题。我正在尝试按照 google 开发人员教程稍作修改。我正在从原始资源文件中读取 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 文件的代码,我从主 activity
调用
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。我不确定为什么,因为它似乎找到了正确的 "employees" 标签!!正如我在调用 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.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.access0(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)
Here's a link to the parser.require() documentation.
public abstract void require (int type, String namespace, String name)
这里中间的一个是我认为导致你的问题的原因。 parser.require
正在寻找正确的标签类型、正确的命名空间和正确的标签名称;如果其中之一不匹配,它将抛出异常。
在您的例子中,对于中间参数,您使用 getName 获取当前标签的名称 ("employees") 并要求 parser.require() 检查是否匹配 命名空间。 Namespace 与标签名称不同,因此会抛出异常。
使用parser.require()
时不需要自己使用getName
,只需要传入你期望的名称即可。
根据文档 "null will match any namespace and any name"。如果你正在做的是检查你有 <employees>
开始标签,那么你可以通过使用 null
作为命名空间参数来忽略命名空间部分:
parser.require(XmlPullParser.START_TAG, null, "employees");
这将检查它是一个开始标签,并且标签名称是 "employees"。
大家好,我遇到了 xmlPullParser 的问题。我正在尝试按照 google 开发人员教程稍作修改。我正在从原始资源文件中读取 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 文件的代码,我从主 activity
调用 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。我不确定为什么,因为它似乎找到了正确的 "employees" 标签!!正如我在调用 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.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.access0(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)
Here's a link to the parser.require() documentation.
public abstract void require (int type, String namespace, String name)
这里中间的一个是我认为导致你的问题的原因。 parser.require
正在寻找正确的标签类型、正确的命名空间和正确的标签名称;如果其中之一不匹配,它将抛出异常。
在您的例子中,对于中间参数,您使用 getName 获取当前标签的名称 ("employees") 并要求 parser.require() 检查是否匹配 命名空间。 Namespace 与标签名称不同,因此会抛出异常。
使用parser.require()
时不需要自己使用getName
,只需要传入你期望的名称即可。
根据文档 "null will match any namespace and any name"。如果你正在做的是检查你有 <employees>
开始标签,那么你可以通过使用 null
作为命名空间参数来忽略命名空间部分:
parser.require(XmlPullParser.START_TAG, null, "employees");
这将检查它是一个开始标签,并且标签名称是 "employees"。