使用XMLPullParser解析XML时出现异常

时间:2013-07-29 09:38:50

标签: java android xml-parsing xmlpullparser

我需要你的帮助来解析XML宣布。

让我们看看下面的XML:

<?xml version="1.0" encoding="UTF-8"?>
<FAVORIS>
   <LOGGED>1</LOGGED>
   <NOTICES NUM_PAGE="2">
      <NOTICE>
         <DEPARTMENT>33</DEPARTMENT>
         <SOURCE>EMP</SOURCE>
         <OBJET><![CDATA[Fourniture d'une solution régionale]]></OBJET>
         <ID_PROCEDURE>7543</ID_PROCEDURE>
         <ORGANISME><![CDATA[Gcs]]></ORGANISME>
         <TYPE_AVIS>ZZ</TYPE_AVIS>
         <TYPE_PROCEDURE>W3</TYPE_PROCEDURE>
         <CATEGORIE>Service</CATEGORIE>
         <DATE_OFFRE>16/09/2013 12:00</DATE_OFFRE>
         <DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
      </NOTICE>
      <NOTICE>
         <DEPARTMENT>33</DEPARTMENT>
         <SOURCE>EMP</SOURCE>
         <OBJET><![CDATA[Refonte du portail]]></OBJET>
         <ID_PROCEDURE>4323</ID_PROCEDURE>
         <ORGANISME><![CDATA[Mairie de W]]></ORGANISME>
         <TYPE_AVIS>Z</TYPE_AVIS>
         <TYPE_PROCEDURE>W1</TYPE_PROCEDURE>
         <CATEGORIE>Service</CATEGORIE>
         <DATE_OFFRE>03/09/2013 12:00</DATE_OFFRE>
         <DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
      </NOTICE>
....
   </NOTICES>
</FAVORIS>

为了解析这个XML,我遵循了http://developer.android.com/training/basics/network-ops/xml.html

中关于XML解析的教程

当我解析这个XML时,我遇到如下错误:

expected: START_TAG {null}NOTICES (position:START_TAG <FAVORIS>@2:10 in java.io.InputStreamReader@40e69360) 

这是由于要求但我该如何解决这个问题?

有关我的代码的更多信息:

public class AOThumbnail_ParserXML {

private static final String ns = null;

public ArrayList<AOThumbnail> parse(String in) throws XmlPullParserException, IOException {

    Decoder decoder = new Decoder();
    String decoded_in = decoder.decode(in);

    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    parser.setInput(new ByteArrayInputStream(decoded_in.getBytes()), null);
    parser.nextTag();
    return readFeed(parser);
}

private ArrayList<AOThumbnail> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    ArrayList<AOThumbnail> entries = new ArrayList<AOThumbnail>();

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

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

        // Starts by looking for the entry tag
        if (name.equals("NOTICE")) {
            entries.add(readEntry(parser));
        } else {
            skip(parser);
        }
    }
    return entries;
}

private AOThumbnail readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {

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

    String organisme = null;
    String objet = null;
    String date_candidature = null;
    String date_offre = null;
    String departement = null;
    String source = null;
    String idProcedure = null;
    String decalageHoraire = null;

    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("ORGANISME")) {
            organisme = read(parser, "ORGANISME");
        } else if (name.equals("OBJET")) {
            objet = read(parser, "OBJET");
        } else if (name.equals("DATE_CAND")) {
            date_candidature = read(parser, "DATE_CAND");
        } else if (name.equals("DATE_OFFRE")) {
            date_offre = read(parser, "DATE_OFFRE");
        } else if (name.equals("DEPARTMENT")) {
            departement = read(parser, "DEPARTMENT");
        } else if (name.equals("SOURCE")) {
            source = read(parser, "SOURCE");
        } else if (name.equals("ID_PROCEDURE")) {
            idProcedure = read(parser, "ID_PROCEDURE");
        } else if (name.equals("DECALAGE_HORAIRE")) {
            decalageHoraire = read(parser, "DECALAGE_HORAIRE");
        } else {
            skip(parser);
        }
    }

    String date = "";
    try {
        if (date_candidature.length() > 0)
            date = date_candidature;
        else if (date_offre.length() > 0)
            date = date_offre;
        else
            date = "Se référer à l'annonce";
    } catch (Exception ex) {
        Logger.logit(ex);
    }

    return new AOThumbnail(organisme, objet, date, departement, idProcedure, source, Boolean.parseBoolean(decalageHoraire));
}

private String read(XmlPullParser parser, String balise) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, balise);
    String content = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, balise);
    return content;
}

private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
        }
    }
}
}

如果我解析这样的XML(见下文),它可以工作,但不适用于上面的XML!

<NOTICES NUM_PAGE="2">
      <NOTICE>
         <DEPARTMENT>33</DEPARTMENT>
         <SOURCE>EMP</SOURCE>
         <OBJET><![CDATA[Fourniture d'une solution régionale]]></OBJET>
         <ID_PROCEDURE>7543</ID_PROCEDURE>
         <ORGANISME><![CDATA[Gcs]]></ORGANISME>
         <TYPE_AVIS>ZZ</TYPE_AVIS>
         <TYPE_PROCEDURE>W3</TYPE_PROCEDURE>
         <CATEGORIE>Service</CATEGORIE>
         <DATE_OFFRE>16/09/2013 12:00</DATE_OFFRE>
         <DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
      </NOTICE>
      <NOTICE>
         <DEPARTMENT>33</DEPARTMENT>
         <SOURCE>EMP</SOURCE>
         <OBJET><![CDATA[Refonte du portail]]></OBJET>
         <ID_PROCEDURE>4323</ID_PROCEDURE>
         <ORGANISME><![CDATA[Mairie de W]]></ORGANISME>
         <TYPE_AVIS>Z</TYPE_AVIS>
         <TYPE_PROCEDURE>W1</TYPE_PROCEDURE>
         <CATEGORIE>Service</CATEGORIE>
         <DATE_OFFRE>03/09/2013 12:00</DATE_OFFRE>
         <DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
      </NOTICE>
....
   </NOTICES>

我希望你能帮助我, 非常感谢!

2 个答案:

答案 0 :(得分:1)

在readFeed()方法中,您告诉您的Parser当前事件应该是名为“NOTICES”的START_TAG,在其他情况下,该方法将抛出一个Exception,这是您发布的。这就是第二个.xml文件工作的原因,但不是第一个(该文件的第一个事件是START_TAG,名称为“FAVORIS”)。

所以你应该用另一种方法来查找NOTICES块,你可以使用这样的东西(还有其他方法):

while (parser.next() != XmlPullParser.START_TAG && !(parser.getName().equals("NOTICES")));

因此,当while循环结束时,您的解析器将位于NOTICES块的开头。

PS:我没有测试代码,所以它可能不起作用,但解决方案应该类似。

答案 1 :(得分:0)

使用require“FAVORIS”解决了问题!

private ArrayList<AOThumbnail> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
        ArrayList<AOThumbnail> entries = new ArrayList<AOThumbnail>();

        //Go to FAVORIS' level
        parser.require(XmlPullParser.START_TAG, ns, "FAVORIS");

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

            String name = parser.getName();
            if(parser.getName().equals("NOTICES")) {
                //Go to NOTICES' level
                parser.require(XmlPullParser.START_TAG, ns, "NOTICES");
                //Parse on Notices' level
                return readFeed(parser);
            }

            // Starts by looking for the entry tag
            if (name.equals("NOTICE")) {
                Logger.logit("Dans notice !!!!!!");
                entries.add(readEntry(parser));
            } else {
                skip(parser);
            }
        }
        return entries;
    }

感谢@ javi9375的提示;)