【问题标题】:Parser is reading tags not in the XML file解析器正在读取不在 XML 文件中的标签
【发布时间】:2023-03-09 01:57:02
【问题描述】:

所以,我正在尝试解析这个 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ButtonDescs</key>
    <array>
        <string>See more information</string>
        <string>Talk to a sales person now</string>
        <string>Question about this vehicle?</string>
        <string>Schedule a Test Drive</string>
        <string>Get a quote for this vehicle</string>
        <string>Apply for a loan</string>
        <string>Calculate monthly payment</string>
        <string>Vehicle History Report</string>
    </array>
    <key>ButtonTitles</key>
    <array>
        <string>More Details</string>
        <string>Call Us</string>
        <string>Email Us</string>
        <string>Test Drive</string>
        <string>Get a Quote</string>
        <string>Finance Request</string>
        <string>Loan Calculators</string>
        <string>CarFax®</string>
    </array>
    <key>ButtonTypes</key>
    <array>
        <string>details</string>
        <string>phone</string>
        <string>vehicle_question</string>
        <string>test_drive</string>
        <string>quote</string>
        <string>credit_form</string>
        <string>loan_calculators</string>
        <string>carfax</string>
    </array>
</dict>
</plist>

这是我正在使用的解析器:

XmlPullParserFactory factory;
    try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        InputStreamReader inputStreamReader = new InputStreamReader(getUrlData(context, url));
        xpp.setInput(inputStreamReader);
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
              if(eventType == XmlPullParser.START_TAG) {
                  if(xpp.getName().equalsIgnoreCase("key")) {
                      xpp.next();
                      if(xpp.getText().equalsIgnoreCase("ButtonDescs"))
                          btnMembers = ButtonMembers.DESCS;
                      else if(xpp.getText().equalsIgnoreCase("ButtonTitles"))
                          btnMembers = ButtonMembers.TITLES;
                      else if(xpp.getText().equalsIgnoreCase("ButtonTypes"))
                          btnMembers = ButtonMembers.TYPES;
                  } else if(xpp.getName().equalsIgnoreCase("array")) {
                      i = 0;
                  } else if(xpp.getName().equalsIgnoreCase("string")) {
                      if(btnMembers == ButtonMembers.DESCS) {
                          button = new InventoryDetailButton();
                          xpp.next();
                          button.setDescription(xpp.getText());
                          btnList.add(button);
                      } else if(btnMembers == ButtonMembers.TITLES) {
                          xpp.next();
                          btnList.get(i).setTitle(xpp.getText());
                      } else if(btnMembers == ButtonMembers.TYPES) {
                          xpp.next();
                          btnList.get(i).setType(xpp.getText());
                      }
                  }
              } else if(eventType == XmlPullParser.END_TAG) {
                  if(xpp.getName().equalsIgnoreCase("string")) {
                      i++;
                  } else if(xpp.getName().equalsIgnoreCase("array") && btnMembers == ButtonMembers.TYPES) {
                      return btnList;
                  }
              }
              eventType = xpp.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我可以将 URL 直接复制并粘贴到浏览器中,我将获得上面粘贴的 XML。但是,当解析器开始检查标签时,它不是预期的标签。我可以逐步完成该过程,xpp 对象的名称将类似于 htmlmetascript。一旦到达script 标签,解析器就会崩溃。

有人知道这些标签可能来自哪里吗?

【问题讨论】:

  • 您是否尝试在浏览器中查看您所说的 XML 页面的源代码?
  • 有几个 PLIST 解析器库。快速的 Google 会找到它们。

标签: android xml parsing


【解决方案1】:

有人知道这些标签可能来自哪里吗?

来自您的 Web 服务器,它无法识别您的请求并返回一个网页,可能带有某种形式的错误信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多