Unmarshalling XML with JAXB without unescaping characters

后端 未结 1 1853
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 05:35

imagine following situation: we receive a xml file from some external tool. Lately within this xml, there can be some escaped charakters in nodenames or within their richcon

相关标签:
1条回答
  • 2021-01-14 06:06

    You need only to replace &# by &# hence call

    unmarshaller.unmarshal(new AmpersandingStream(new FileInputStream(...)));
    

    and

    import java.io.IOException;
    import java.io.InputStream;
    
    /**
    * Replaces numerical entities with their notation as text.
    */
    public class AmpersandingStream extends InputStream {
    
        private InputStream in;
        private boolean justReadAmpersand;
        private String lookAhead = "";
    
        public AmpersandingStream(InputStream in) {
            this.in = in;
        }
    
        @Override
        public int read() throws IOException {
            if (!lookAhead.isEmpty()) {
                int c = lookAhead.codePointAt(0);
                lookAhead = lookAhead.substring(Character.charCount(c));
                return c;
            }
            int c = in.read();
            if (c == (int)'#' && justReadAmpersand) {
                c = (int)'a';
                lookAhead = "mp;#";
            }
            justReadAmpersand = c == (int)'&';
            return c;
        }
    
        @Override
        public int available() throws IOException {
            return in.available();
        }
    
        @Override
        public void close() throws IOException {
            in.close();
        }
    
        @Override
        public synchronized void mark(int readlimit) {
            in.mark(readlimit);
        }
    
        @Override
        public boolean markSupported() {
            return in.markSupported();
        }
    
        @Override
        public int read(byte[] b) throws IOException {
            return in.read(b);
        }
    
        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            return in.read(b, off, len);
        }
    
        @Override
        public synchronized void reset() throws IOException {
            in.reset();
        }
    
        @Override
        public long skip(long n) throws IOException {
            return in.skip(n);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题