خرید بک لینک

Vote count: 0

i have a problem with parsing of complicated (for me) xml. I have this xml:

<output>
<errorCode>0</errorCode>
 <contents>
  <P descrizione=" Tutte " codice="0">
    <A ao="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 ao="2016">
    <M totale="3029" mese="1"/>
    <M totale="803" mese="2"/>
   </A>
  </P>
  <P descrizione="Teis" codice="558">
    <A ao="2015">
      <M totale="1" mese="12"/>
    </A>
  </P>
  <P descrizione="Soccer" codice="561">
    <A ao="2015">
      <M totale="2" mese="7"/>
    </A>
  </P>
 ....
 ....
 <contents>
<output>

I used Xml pull parser to parse this and i would create object likt this xml.

This is the code that i created to read 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();
        retu 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);
        }
    }
    retu 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");
    retu 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");
    retu 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");
    retu 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);
        }
    }
    retu 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);
        }
    }
    retu 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);
        }
    }
    retu 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);
        }
    }
    retu 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);
            }
        }
    retu 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);
        }
    }
    retu valueInseredTicket;
}

But i received error java.lang.IndexOutOfBoundsException. I ddon't know how to implement correctly my class to parse this xml. Please help me. Thank You

asked 25 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 4 تير 1395 ساعت: 17:55

صفحه بندی