修剪 XML 时出现内存不足错误

发布于 2024-11-02 18:30:25 字数 1275 浏览 2 评论 0原文

我正在编写一个从 http 解析 XML 文件的程序。 XML 的根标签前面有一些空格。

因此,我需要在解析 XML 之前对其进行修剪。

这是我写的方法,

     private String trimXML(InputStream inputStream){
        Writer writer = null;
        try{
            writer = new StringWriter();
            char[] buffer = new char[Constants.BUFFER_SIZE];
            try {
                Reader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    int n = 0;                
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return writer.toString().trim();
        }catch(NullPointerException e){
            return null;
        }
    }

现在我面临一个大问题是在我请求XML几次后,出现内存不足错误...

有人能给我一些建议来解决它吗?

I am doing a program to parse a XML file from http.
And the XML have some space at the front of root tag.

So, I need to trim the XML before parse it.

Here is the method I written,

     private String trimXML(InputStream inputStream){
        Writer writer = null;
        try{
            writer = new StringWriter();
            char[] buffer = new char[Constants.BUFFER_SIZE];
            try {
                Reader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    int n = 0;                
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return writer.toString().trim();
        }catch(NullPointerException e){
            return null;
        }
    }

Now I am facing a big problem is after I request the XML few times, the out of memory error is get...

Could anyone give me some suggestion to solve it?

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

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

发布评论

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

评论(4

软甜啾 2024-11-09 18:30:25

为什么? XML 解析器不关心空白。如果您收到无法解析的 XML 文档,解决方案是修复发送方,而不是在接收方乱搞一些东西。这样一来大家都错了。

Why? XML parser's don't care about white space. And if you are receiving an XML document that doesn't parse, the solution is to fix the sender, not kludge something up at the receiver. That way everybody is wrong.

笑脸一如从前 2024-11-09 18:30:25

您正在使用StringWriter,这意味着您不断将XML 文件内容写入内存中的缓冲区。因此,显然,如果最后一个缓冲区保留在内存中并且尚未被垃圾回收,您将遇到 OutOfMemory

顺便说一句,我不明白你的程序是如何解决你的问题的。它只是修剪整个文档。这意味着它将清除文档开头和结尾的空白。

我给您的建议是,您不必担心空白并使用 StAX 或任何适合您的方式解析文档。如有必要,请在解析过程中修剪该内容。

然而,冲洗可能会有所帮助。因此,快速修复可以是这样,

..
..
String str = writer.toString().trim();
writer.flush();
return str;
..
..

注意: XML 元素周围的空格仍然会被解析器忽略。如果您需要修剪某些属性/元素值,那就是另一回事了。

You are using StringWriter, which means you keep on writing the XML file content to the buffer in memory. So, obviously, if the last buffer stays in the memory and not garbage collected yet, you will hit OutOfMemory.

By the way, I am not understanding how your program is solving your problem. It is merely trimming the whole document. Which means it will clear the white-spaces from the beginning and the end of the document.

My suggestion to you is, you need not worry about the white space and parse the document using StAX or whatever feel appropriate to you. And trim the thing during parsing, if necessary.

However, flushing might help. Thus, a quick fix can be this,

..
..
String str = writer.toString().trim();
writer.flush();
return str;
..
..

NB: white-spaces around XML elements will be ignored by the parser, nonetheless. If you need to trim some attributes/elements values, that's another story.

夢归不見 2024-11-09 18:30:25

我可以建议您也许正在尝试解决一个已经解决的问题吗?解析器需要自己写吗?

我建议不要尝试自己解析 XML,而是使用像 Simple XML 库这样的库,适用于 Android。我实际上刚刚写了一篇博客文章,解释如何将其包含在您的一个项目中: 您可以在这里找到

Can I suggest that perhaps you are trying to solve a problem that has already been solved? Do you need to write the parser yourself?

What I would suggest is to not try and parse the XML yourself, but use a library like the Simple XML library which works on Android. I actualy just wrote a blog post explaining how to include it in one of your projects: you can find that here.

甩你一脸翔 2024-11-09 18:30:25

您可以将 InputStream 前进到第一次出现 '<'使用类似的东西:

    InputStream inputStream = new BufferedInputStream(YOUR_INPUT_STREAM);
    byte[] start = "<".getBytes("UTF-8");
    byte[] potentialStart = new byte[1];

    inputStream.read(potentialStart);
    while(start[0] != potentialStart[0]) {
        inputStream.mark(1);
        inputStream.read(potentialStart);
    }
    inputStream.reset();

You could advance the InputStream to the first occurrence of '<' using something like:

    InputStream inputStream = new BufferedInputStream(YOUR_INPUT_STREAM);
    byte[] start = "<".getBytes("UTF-8");
    byte[] potentialStart = new byte[1];

    inputStream.read(potentialStart);
    while(start[0] != potentialStart[0]) {
        inputStream.mark(1);
        inputStream.read(potentialStart);
    }
    inputStream.reset();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文