使用JAXB解组XML而不使用unescaping字符

时间:2012-02-15 15:46:23

标签: java xml escaping unmarshalling

想象下面的情况:我们从一些外部工具收到一个xml文件。最近在这个xml中,可以在节点标签中或在其富含内容标签内有一些转义字符串,如下例所示(简称):

<map>
<node TEXT="Project">
<node TEXT="&#xe4;&#xe4;">
<richcontent TYPE="NOTE"><html>
  <head>

  </head>
  <body>
    <p>
      I am a Note for Node &#228;&#228;!
    </p>
  </body>
</html>
</richcontent>
</node>
</node>
</map>

在用JAXB解组文件之后,那些逃脱的charakters得不到了解。不幸的是,我需要它们保持原样,意味着逃脱。有什么方法可以避免在解组时对这些角色进行转移?

在进行研究时,我发现了许多关于编组xml文件的问题,其中出现了相反的问题,但那些对我没有帮助:

甚至可以用JAXB实现这个目标,还是我们甚至不得不考虑改用不同的xml阅读器API?

提前谢谢你, ymene

1 个答案:

答案 0 :(得分:2)

您只需将&#替换为&amp;#,然后调用

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

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);
    }

}