0

大家好,我遇到了 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) 
4

1 回答 1

1

这是 parser.require() 文档的链接。

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");

这将检查它是一个开始标签,并且标签名称是“员工”。

于 2015-11-17T21:25:45.340 回答