Java用非法XML字符解组xml

问题描述 投票:0回答:1

我正在尝试用javax.xml.bind.Unmarshaller解组XML字符串,但收到以下错误:

Caused by: org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x13) was found in the element content of the document.

是否有任何通用解决方案从输入字符串中删除所有非法XML字符?

例如,我尝试使用以下一个,但它没有帮助:

public static String illegalXML11CharactersPattern = "[^"
        + "\u0001-\uD7FF"
        + "\uE000-\uFFFD"
        + "\ud800\udc00-\udbff\udfff"
        + "]+";

public static String stripNonValidXML11Characters(String xml) {
    return xml.replaceAll(illegalXML11CharactersPattern, "");
}
java xml jaxb jaxb2
1个回答
0
投票

最后,我完成了以下方法:

xml = org.apache.commons.lang3.StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml10(xml));
© www.soinside.com 2019 - 2024. All rights reserved.