Android Retrofit解析xml

时间:2016-01-08 16:30:12

标签: android xml parsing xml-parsing retrofit

我刚刚开始使用Retrofit。如何解析以下XML:

<WTF>
 <RT fast="3">
  <NEW i="98" frag="S" gs="0" gm="1">
   <CRC desc="08" bad="01">
    <XYZ po="77"/>
    <PARSE max="7" min="1"/>
   </CRC>
   <CRC desc="08" bad="01">
    <XYZ po="65"/>
    <PARSE max="5" min="4"/>
   </CRC>
   <CRC desc="09" bad="01">
    <XYZ po="23"/>
    <PARSE max="7" min="5"/>
   </CRC>
   <CRC desc="09" bad="01">
    <XYZ po="47"/>
    <PARSE max="6" min="0"/>
   </CRC>
  </NEW>
 </RT>
</WTF>

这就是我正在做的事情:

首先,我在Retrofit Gradle中添加了

compile ('com.squareup.retrofit:converter-simplexml:2.0.0-beta2') {
            exclude group: 'xpp3', module: 'xpp3'
            exclude group: 'stax', module: 'stax-api'
            exclude group: 'stax', module: 'stax'
}

然后,在主要的活动中我添加了:

 try {
            String xmlData = getXmlFromUrl(url);
            //xmlData here I successfully got xml
            Serializer serializer = new Persister();

            Reader reader = new StringReader(xmlData);
            XML osd =   serializer.read(XML.class, reader, false);
            Log.d(TAG, "-----" +osd.toString());

        } 
        catch (Exception e) {
            Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
        }

在XML课程中:

@Root(name = "WTF")
public class XML {

    @ElementList(name = "RT")
    public List<RT> rt;


    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(rt);
        return builder.toString();
    }
}

我从类继承

在RT课程中:

@Root(name = "RT")

public class RT {

        @ElementList(name = "NEW")
        public List<NEW> nw;


        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append(nw);
            return builder.toString();
        }
}

但不幸的是我一直都是错误。

0 个答案:

没有答案