麻烦用android xmlPullParserException中的xmlPullParser解析本地xml文件

时间:2015-11-17 20:45:30

标签: android xml android-xmlpullparser

大家好我的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;
}

当我调用&#34; parser.require()&#34;时会抛出xmlpullparserexception。在readFeed()方法中。我不确定为什么因为它似乎找到了正确的&#34;员工&#34;标签!!因为我在调用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) 

1 个答案:

答案 0 :(得分:1)

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与标记名称不同,因此会抛出异常。

使用getName时,您无需自己使用parser.require(),只需要传递您期望的名称。

根据文档“null将匹配任何名称空间和任何名称”。如果你正在做的是检查你是否有<employees>开始标记,那么你可以通过使用null作为命名空间参数来忽略命名空间部分:

    parser.require(XmlPullParser.START_TAG, null, "employees");

这将检查它是否为开始标记,并且标记名称为“employees”。