我旁边的xml文件如何逃脱组XML特殊字符JAXB
<xml name="Places">
<data>
<row Code="1" Name="#X1.A&B(City)" />
</data>
</xml>
而且我执行解组我得到Name属性里面,因为符号(&)的例外The reference to entity "B" must end with the ';' delimiter
后。
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
File xml = new File("test2.xml");
Object obj = unmarshaller.unmarshal(xml);
我该如何逃避这些角色?
我已经尝试添加CharacterEscapeHandler,但它不工作的Unmarshaller
private static class EscapeHandler implements CharacterEscapeHandler {
@Override
public void escape(char[] buf, int start, int len, boolean isAttValue,
Writer out) throws IOException {
...
}
}
unmarshaller.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler",
new EscapeHandler());
而且我越来越javax.xml.bind.PropertyException: name: com.sun.xml.bind.marshaller.CharacterEscapeHandler
可能是一个重复:http://stackoverflow.com/a/18017130/506855 虽然这是关于编组而不是反编组。 – Puce 2015-01-15 17:33:01
JAXB实现将取决于较低级别(SAX或StAX)解析器。如果您可以找到一个容忍无效XML的容器,那么您将能够让JAXB使用它。 – 2015-01-15 18:23:11