java为带有“&”字符的字符串创建XMLStreamReader
我想解组一个包含“&;的xmlString性格
XMLInputFactory xif = XmlUtils.newXMLInputFactory();
String test = "<Res>Abc & Def</Res>";
StringReader sr = new StringReader(test);
try {
XMLStreamReader reader = xif.createXMLStreamReader(sr);
JAXBContext jaxbContext = JAXBContext.newInstance(BluelineReprintInvoiceResponseData.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object o = unmarshaller.unmarshal(reader);
} catch(Exception ex) {
ex.printStackTrace();
}
是否有任何方法可以在不使用replace/replaceAll替换“&;”的情况下解组字符串字符串中的字符
# 1 楼答案
基本上没有,因为这是无效的XML。在这种情况下,不允许使用裸符号。任何一致的XML解析器在遇到
&
时都会给您一个解析错误,然后找不到相应的;
正确的解决方案是在源代码处修复(所谓的)XML。以XML的形式生成该字符串的是错误的。如果是人工输入的,则应在输入时验证XML
请注意,如果试图使用字符串替换自动“纠正”损坏的XML中的裸符号,则可能会造成其他损害;i、 e
自动更正代码需要知道符号和出现的上下文。使用正则表达式是很困难的