解析复合类型 XML

发布于 2024-10-17 04:53:59 字数 1685 浏览 3 评论 0原文

我有一个 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 编写解析器。如果您有一些例子,将会有很大的帮助。

谢谢穆库尔

I have a SOAP web service that returns an XML in this format

<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>

How to write a parser for this kind of XML. If you have some examples, it will be of great help.

Thanks

Mukul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-10-24 04:53:59

我让它与这个解析器一起工作——

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("'", "'")+"',");
                } 
                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 多个对象时会耗尽内存。

I made it work with this parser -

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("'", "'")+"',");
                } 
                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();
}

The Stringbuilder i have used to create a string out of the values returned by the XML and use this string directly to write to the database. The parse method takes an inputstream, the application context and a string that helps me build the sql statement. From the parser itself i write directly to the database. Since the XML was very large (7MB), i had to do it this way otherwise my android device would run out of memory while building 1500+ objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文