解析复合类型XML

时间:2011-02-14 09:47:18

标签: xml soap xml-parsing

我有一个SOAP Web服务,它以这种格式返回XML

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:GetResponse>
      <ret SOAP-ENC:arrayType="ns2:Map[2]" xsi:type="SOAP-ENC:Array">
         <item xsi:type="ns2:Map">
           <item>
              <key xsi:type="xsd:string">ProtocolId</key>
              <value xsi:type="xsd:string">1</value>
           </item>
           <item>
              <key xsi:type="xsd:string">Title</key>
              <value xsi:type="xsd:string">Some Title</value>
           </item>
           <item>
              <key xsi:type="xsd:string">Text</key>
              <value xsi:type="xsd:string"> Some Text </value>
           </item>
         </item> 
         <item xsi:type="ns2:Map">
           <item>
              <key xsi:type="xsd:string">ProtocolId</key>
              <value xsi:type="xsd:string">2</value>
           </item>
           <item>
              <key xsi:type="xsd:string">Title</key>
              <value xsi:type="xsd:string">Another Title</value>
           </item>
           <item>
              <key xsi:type="xsd:string">Text</key>
              <value xsi:type="xsd:string">Another Text </value>
           </item>
         </item> 
      </ret>
   </ns1:GetResponse>
 </SOAP-ENV:Body>

如何为这种XML编写解析器。如果你有一些例子,那将会有很大的帮助。

由于

穆库尔

1 个答案:

答案 0 :(得分:2)

我使用了这个解析器 -

public class XmlPullFeedParser extends BaseFeedParser {
public XmlPullFeedParser(String feedUrl) {
    super(feedUrl);
}

StringBuilder builder = new StringBuilder();
int whichItemFlag = 0;

Context thisContext;

DataBaseHelper myDB;

public void parse(InputStream is, Context context, String insertInto) {

    XmlPullParser parser = Xml.newPullParser();

    thisContext = context;
    myDB = new DataBaseHelper(thisContext);
    try {
        // auto-detect the encoding from the stream
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        boolean done = false;
        while (eventType != XmlPullParser.END_DOCUMENT && !done) {
            String name = null;
            String attr = null;
            switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                attr = parser.getAttributeName(0);
                if (name.equalsIgnoreCase(ITEM)) {
                    if(attr!=null) {
                        builder.append("(");
                    }
                    whichItemFlag++;
                } else if (name.equalsIgnoreCase(VALUE)) {
                    builder.append("'"+parser.nextText().replaceAll("'", "&#39;")+"',");
                } 
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase(ITEM)) {
                    whichItemFlag--;
                    if(whichItemFlag==0) {
                        builder.delete(builder.length()-1, builder.length());
                        builder.append(")");
                        writeStringToDb(insertInto, builder.toString());
                        builder.delete(0, builder.length());
                    }
                } 
                break;
            }
            eventType = parser.next();
        } 
    } catch (Exception e) {
        e.printStackTrace();
//          throw new RuntimeException(e);
    } finally {
        myDB.close();
    }
}

private void writeStringToDb(String insertInto, String string) {
    SQLiteDatabase db = myDB.getWritableDatabase();
    String sql = insertInto + string;
    db.execSQL(sql);
    db.close();
}

Stringbuilder我曾经使用XML返回的值创建一个字符串,并直接使用该字符串写入数据库。 parse方法接受一个输入流,应用程序上下文和一个帮助我构建sql语句的字符串。从解析器本身我直接写入数据库。由于XML非常大(7MB),我不得不这样做,否则我的android设备在构建1500多个对象时会耗尽内存。