如何在J2ME中从流中删除无效的XML字符? org.xml.sax.SAXParseException:无效字符 [英] How should I strip invalid XML characters from a stream in J2ME? org.xml.sax.SAXParseException: Invalid character

查看:125
本文介绍了如何在J2ME中从流中删除无效的XML字符? org.xml.sax.SAXParseException:无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在Blackberry JDE v4.2.1上运行.它是通过使Web API调用返回XML的方法来运行的.有时,返回的XML格式不正确,我需要在分析之前去除所有无效字符.

This code is running on Blackberry JDE v4.2.1 It's in a method that makes web API calls that return XML. Sometimes, the XML returned is not well formed and I need to strip out any invalid characters prior to parse.

当前,我得到:org.xml.sax.SAXParseException: Invalid character '' encountered.

我希望看到一种在输入流上附加无效字符剥离器的快速方法的构想,以使该流仅流经验证器/剥离器并进入解析调用.也就是说,我正在努力避免保存流中的内容.

I would like to see ideas of a fast way to attach an invalid character stripper on the input stream so that the stream just flows through the validator/stripper and into the parse call. i.e. I'm trying to avoid saving the content of the stream.

现有代码:

处理程序是对DefaultHandler
的替代 url 是包含API URL

handler is an override of DefaultHandler
url is a String containing the API URL

hconn = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);

...

try{
   XMLParser parser = new XMLParser();
   InputStream input = hconn.openInputStream();
   parser.parse(input, handler);
   input.close();
} catch (SAXException e) {
   Logger.getInstance().error("getViaHTTP() - SAXException - "+e.toString());
}

推荐答案

由于流是面向字节的,因此很难在InputStream上附加剥离器.在 Reader .您可以制作类似StripReader的东西,该包裹包装另一个阅读器并处理错误.以下是对此的快速未经测试的概念证明:

It's difficult to attach a stripper on the InputStream because streams are byte-oriented. It might make more sense to do it on a Reader. You could make something like a StripReader that wraps a another reader and deals with errors. Below is a quick, untested, proof of concept for this:

public class StripReader extends Reader
{
    private Reader in;
    public StripReader(Reader in)
    {
    this.in = in;
    }

    public boolean markSupported()
    {
    return false;
    }

    public void mark(int readLimit)
    {
    throw new UnsupportedOperationException("Mark not supported");
    }

    public void reset()
    {
    throw new UnsupportedOperationException("Reset not supported");
    }

    public int read() throws IOException
    {
    int next;
    do
    {
        next = in.read();
    } while(!(next == -1 || Character.isValidCodePoint(next)));

    return next; 
    }

    public void close() throws IOException
    {
    in.close();
    }

    public int read(char[] cbuf, int off, int len) throws IOException
    {
    int i, next = 0;
    for(i = 0; i < len; i++)
    {
        next = read();
        if(next == -1)
        break;
        cbuf[off + i] = (char)next;
    }
    if(i == 0 && next == -1)
        return -1;
    else
        return i;
    }

    public int read(char[] cbuf) throws IOException
    {
    return read(cbuf, 0, cbuf.length);
    }
}

然后,您将构造一个 InputSource ,然后Reader使用InputSource进行解析.

You would then construct an InputSource from then Reader then do the parse using the InputSource.

这篇关于如何在J2ME中从流中删除无效的XML字符? org.xml.sax.SAXParseException:无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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