Android XmlPullParser使用更多List和Map解析Xml

时间:2016-06-24 10:41:19

标签: java android xml xmlpullparser

我解析复杂的(对我来说)xml有问题。 我有这个xml:

<output>
<errorCode>0</errorCode>
 <contents>
  <P descrizione=" Tutte " codice="0">
    <A anno="2015">
     <M totale="579" mese="6"/>
     <M totale="1313" mese="7"/>
     <M totale="354" mese="8"/>
     <M totale="851" mese="9"/>
     <M totale="1397" mese="10"/>
     <M totale="1960" mese="11"/>
     <M totale="3144" mese="12"/>
   </A>
   <A anno="2016">
    <M totale="3029" mese="1"/>
    <M totale="803" mese="2"/>
   </A>
  </P>
  <P descrizione="Tennis" codice="558">
    <A anno="2015">
      <M totale="1" mese="12"/>
    </A>
  </P>
  <P descrizione="Soccer" codice="561">
    <A anno="2015">
      <M totale="2" mese="7"/>
    </A>
  </P>
 ....
 ....
 <contents>
<output>

我使用Xml pull解析器来解析这个,我会创建对象这个xml。

这是我创建的用于读取xml的代码:

....
 public static class Output {

        public static String errorCode;
        public static List<PDescription> contents;

        public static class PDescription {

            public static List<AYears> pDescription;

            public static class AYears {

                public static List<Attributes> aYears;

                public static class Attributes {

                    public static Map<String, String> attribute;
                }
            }
        }
    }

 public  XmlServiceLevel parse(InputStream in) throws XmlPullParserException, IOException {
    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readXmlServiceLevel(parser);
    }finally {
        in.close();
    }
}

private XmlServiceLevel readXmlServiceLevel(XmlPullParser parser) throws XmlPullParserException, IOException {
    XmlServiceLevel serviceLevel = new XmlServiceLevel();

    parser.require(XmlPullParser.START_TAG, ns, "xml");
    while (parser.next() != XmlPullParser.END_TAG){
        if (parser.getEventType() != XmlPullParser.START_TAG){
            continue;
        }
        String nameTag = parser.getName();
        if (nameTag.equals("protocol")){
            XmlServiceLevel.protocol = readProtocol(parser);
        } else if (nameTag.equals("system")) {
            XmlServiceLevel.system = readSystem(parser);
        } else if (nameTag.equals("release")) {
            XmlServiceLevel.release = readRelease(parser);
        } else if (nameTag.equals("output")) {
            XmlServiceLevel.output = readOutput(parser);
        } else {
            skip(parser);
        }
    }
    return serviceLevel;
}

private String readProtocol(XmlPullParser parser) throws XmlPullParserException, IOException {

    parser.require(XmlPullParser.START_TAG, ns, "protocol");
    String protocol = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "protocol");
    return protocol;
}

private String readSystem(XmlPullParser parser) throws XmlPullParserException, IOException {

    parser.require(XmlPullParser.START_TAG, ns, "system");
    String system = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "system");
    return system;
}

private String readRelease(XmlPullParser parser) throws XmlPullParserException, IOException {

    parser.require(XmlPullParser.START_TAG, ns, "release");
    String release = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "release");
    return release;
}

private XmlServiceLevel.Output readOutput(XmlPullParser parser) throws XmlPullParserException, IOException {
    XmlServiceLevel.Output output = new XmlServiceLevel.Output();

    parser.require(XmlPullParser.START_TAG, ns, "output");
    while (parser.next() != XmlPullParser.END_TAG){
        if (parser.getEventType() != XmlPullParser.START_TAG){
            continue;
        }
        String nameTag = parser.getName();
        if (nameTag.equals("errorCode")) {
            output.errorCode = readErrorCode(parser);
        } else if (nameTag.equals("contents")){
            output.contents = readContents(parser);
        } else {
            skip(parser);
        }
    }
    return output;
}

private List<XmlServiceLevel.Output.PDescription> readContents(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<XmlServiceLevel.Output.PDescription> pDescription = new ArrayList<>();

    parser.require(XmlPullParser.START_TAG, ns, "contents");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String nameTag = parser.getName();
        if (nameTag.equals("P")) {
            XmlServiceLevel.Output.PDescription.pDescription.add(readPDescritpion(parser));
        } else {
            skip(parser);
        }
    }
    return pDescription ;
}

private XmlServiceLevel.Output.PDescription.AYears readPDescritpion (XmlPullParser parser) throws XmlPullParserException, IOException {
    XmlServiceLevel.Output.PDescription.AYears aYears = new XmlServiceLevel.Output.PDescription.AYears();
    XmlServiceLevel.Output.PDescription.AYears.aYears = new ArrayList<>();

    parser.require(XmlPullParser.START_TAG, ns, "P");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String nameTag = parser.getName();
        if (nameTag.equals("A")) {
            XmlServiceLevel.Output.PDescription.AYears.aYears.add(readAYear(parser));
        } else {
            skip(parser);
        }
    }
    return aYears;
}

private XmlServiceLevel.Output.PDescription.AYears.Attributes readAYear(XmlPullParser parser) throws XmlPullParserException,IOException {
    XmlServiceLevel.Output.PDescription.AYears.Attributes attributes = new XmlServiceLevel.Output.PDescription.AYears.Attributes();
    XmlServiceLevel.Output.PDescription.AYears.Attributes.attribute = new HashMap<>();

    parser.require(XmlPullParser.START_TAG, ns, "A");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String tagName = parser.getName();

        int tagCount = parser.getAttributeCount();
        if (tagName.equals("M")) {
            XmlServiceLevel.Output.PDescription.AYears.Attributes.attribute.put(readMonths(parser),readValueInseredTicket(parser));
            parser.nextTag();
        } else {
            skip(parser);
        }
    }
    return attributes;
}

private String readMonths (XmlPullParser parser) throws XmlPullParserException, IOException {
    XmlServiceLevel.Output.PDescription.AYears.Attributes.attribute = new HashMap<>();

    parser.require(XmlPullParser.START_TAG, ns, "M");
    String tagName = parser.getName();
    String monthValue = null;
    while (parser.next() == XmlPullParser.END_TAG) {
            if (tagName.equals("M")) {
                monthValue = parser.getAttributeValue(0);
            }
        }
    return monthValue;
}

private String readValueInseredTicket (XmlPullParser parser) throws XmlPullParserException, IOException {
    XmlServiceLevel.Output.PDescription.AYears.Attributes.attribute = new HashMap<>();

    parser.require(XmlPullParser.START_TAG, ns, "M");
    String tagName = parser.getName();
    String valueInseredTicket = null;
    while (parser.next() == XmlPullParser.END_TAG) {
        if (tagName.equals("M")) {
            valueInseredTicket = parser.getAttributeValue(1);
        }
    }
    return valueInseredTicket;
}

但我收到错误java.lang.IndexOutOfBoundsException。我不知道如何正确地实现我的类来解析这个xml。请帮我。 谢谢

0 个答案:

没有答案