1

我正在尝试解析 XML 文件,但我遇到了文档问题!

我对这条线有疑问:

parser.require(XmlPullParser.START_TAG, ns, "CarStatus");

但是,我得到的是 null 而不是 START_TAG。

XML HTTP POST 响应:

06-20 15:15:40.804: I/RESPONSE(2270): <?xml version="1.0" encoding="utf-8"?>
06-20 15:15:40.804: I/RESPONSE(2270): <CarObject>
06-20 15:15:40.804: I/RESPONSE(2270):   <ResultCode>0</ResultCode>
06-20 15:15:40.804: I/RESPONSE(2270):   <ResultMessage>success</ResultMessage>
06-20 15:15:40.804: I/RESPONSE(2270):   <Cardata xsi:type="ArrayOfCarStatus">
06-20 15:15:40.804: I/RESPONSE(2270):     <CarStatus> 
06-20 15:15:40.804: I/RESPONSE(2270):       <CarID>241</CarID>
06-20 15:15:40.804: I/RESPONSE(2270):       <CarName>ASC</CarName>
06-20 15:15:40.804: I/RESPONSE(2270):     </CarStatus>
06-20 15:15:40.804: I/RESPONSE(2270):   </CarData>
06-20 15:15:40.804: I/RESPONSE(2270): </CarObject>

这是我遇到问题的代码。

private CarStatus readCarStatus(XmlPullParser parser)
            throws XmlPullParserException, IOException {

        parser.require(XmlPullParser.START_TAG, ns, "CarStatus");
        String carId = null;
        String carName = null;

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }

            String name = parser.getName();
            if (name.equals("CarID")) {
                carId = readCarId(parser);
            } else if (name.equals("CarName")) {
                carName = readCarName(parser);
            } else {
                skip(parser);
            }
        }
        return new CarStatus(carId, carName);
    }

06-20 16:56:15.773: W/System.err(4017): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}CarStatus (position:START_TAG <ResultObject >@2:146 in java.io.InputStreamReader@415f04d8) 
06-20 16:56:15.773: W/System.err(4017):     at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
06-20 16:56:15.773: W/System.err(4017):     at com.bentseytlin.maps.MainActivity.readCarStatus(MainActivity.java:174)
06-20 16:56:15.773: W/System.err(4017):     at com.bentseytlin.maps.MainActivity.parse(MainActivity.java:143)
06-20 16:56:15.773: W/System.err(4017):     at com.bentseytlin.maps.MainActivity.onCreate(MainActivity.java:88)
06-20 16:56:15.773: W/System.err(4017):     at android.app.Activity.performCreate(Activity.java:5104)
06-20 16:56:15.780: W/System.err(4017):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-20 16:56:15.780: W/System.err(4017):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-20 16:56:15.780: W/System.err(4017):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-20 16:56:15.780: W/System.err(4017):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
06-20 16:56:15.780: W/System.err(4017):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-20 16:56:15.780: W/System.err(4017):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
06-20 16:56:15.780: W/System.err(4017):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 16:56:15.780: W/System.err(4017):     at android.os.Looper.loop(Looper.java:137)
06-20 16:56:15.780: W/System.err(4017):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 16:56:15.780: W/System.err(4017):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 16:56:15.780: W/System.err(4017):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 16:56:15.780: W/System.err(4017):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 16:56:15.780: W/System.err(4017):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 16:56:15.780: W/System.err(4017):     at dalvik.system.NativeStart.main(Native Method)

谢谢您的帮助!

4

1 回答 1

0

我认为你使用require不正确。我感觉你希望它在START_TAG等于时告诉你<CarStatus>。但是在每次检查失败时,require 方法都会抛出一个异常,这意味着你必须添加一个 try-catch 块,并且循环会有点奇怪。请参阅android 文档以获取更多信息。

我个人不会使用 reqiure 来做,而是像我在上面的评论中写的那样(这个线程)。然而,我必须承认我已经有一段时间没有这样做了。:-)

于 2013-06-24T08:30:15.170 回答