代码之家  ›  专栏  ›  技术社区  ›  JR Lawhorne

在J2ME中,我应该如何从流中删除无效的XML字符?org.xml.sax。SAXParseException:无效字符

  •  1
  • JR Lawhorne  · 技术社区  · 17 年前

    org.xml.sax.SAXParseException: Invalid character '' encountered .

    现有代码:

    DefaultHandler
    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());
    }
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Matthew Flaschen    17 年前

    很难在InputStream上附加剥离器,因为流是面向字节的。在a上做这件事可能更有意义 Reader

    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

        2
  •  0
  •   alphazero    17 年前