想象下面的情况:我们从一些外部工具收到一个xml文件.最近在这个xml中,nodenames或richcontent标签中可能存在一些转义字符,如下例所示(简称):
<map>
<node TEXT="Project">
<node TEXT="ää">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<body>
<p>
I am a Note for Node ää!
</p>
</body>
</html>
</richcontent>
</node>
</node>
</map>
在用JAXB解组文件之后,那些逃脱的charakters得不到了解.不幸的是,我需要它们保持原样,意味着逃脱.有什么办法可以避免在解组的时候不使用这些角色吗?
在研究的过程中,我发现了许多关于编组xml文件的问题,其中出现了相反的问题,但那些对我没有帮助:
甚至可以用JAXB实现这个目标,还是我们甚至不得不考虑改用不同的xml阅读器API?
先感谢您,
ymene
解决方法:
您只需要替换&#by& amp;#since call
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);
}
}