解析Android中大xml文件 [英] Parse big xml file in android

查看:101
本文介绍了解析Android中大xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序有一个搜索选项。如果用户输入搜索值,我得从WebService的值。我得到一个大的web服务的价值。在我的web服务的字符串值都来了。我越来越喜欢<>如XML字符实体引用等。我想替换所有字符和解析XML。谁能告诉我如何做到这一点,举个例子?

In my application there is a search option. If the user enters a search value, I have to get a value from the webservice. I am getting a large webservice value. In my webservice string values are coming. I am getting like <> like xml character entity reference like. I want to replace all characters and parse xml. Can anybody tell me how to do this and give an example?

我用的StringBuffer 试过unescapexml的性格,我得到了内存不足的错误

I tried with StringBuffer for unescapexml character, I am getting out of memory error

public  String unescapeXML(String str) {
        if (str == null || str.length() == 0)
          return "";

        StringBuffer buf = new StringBuffer();
        int len = str.length();
        for (int i = 0; i < len; ++i) {
          char c = str.charAt(i);
          if (c == '&') {
            int pos = str.indexOf(";", i);
            if (pos == -1) { // Really evil
              buf.append('&');
            } else if (str.charAt(i + 1) == '#') {
              int val = Integer.parseInt(str.substring(i + 2, pos), 16);
              buf.append((char) val);
              i = pos;
            } else {
              String substr = str.substring(i, pos + 1);
              if (substr.equals("&amp;"))
                buf.append('&');
              else if (substr.equals("&lt;"))
                buf.append('<');
              else if (substr.equals("&gt;"))
                buf.append('>');
              else if (substr.equals("&quot;"))
                buf.append('"');
              else if (substr.equals("&apos;"))
                buf.append('\'');
              else if (substr.equals("&nbsp;"))
                  buf.append(" ");

              else
                // ????
                buf.append(substr);
              i = pos;
            }
          } else {
            buf.append(c);
          }
        }
        return buf.toString();
      }

我试过流,我无法做到这一点。任何人都可以举一个例子,如何做到这一点?

I tried with stream, I am not able to do it. Can anybody give an example how to do this?

推荐答案

您不应该分析它,你自己的。有更好的方法 - SAX或DOM。
这个资源包含了很多关于这两种方式有用inforamtion(以及code例子太):的 http://onjava.com/pub/a/onjava/2002/06/26/xml.html

You should not parse it on your own. There are better ways - SAX or DOM. This resource contains a lot of useful inforamtion about these both ways (and code examples too): http://onjava.com/pub/a/onjava/2002/06/26/xml.html

这篇关于解析Android中大xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆