您将得到一个字符串,其中包含任何类型的字符 (UTF-8),包括表情符号/表情符号 👍 🏁 等特殊字符。 您必须生成一个包含接收到的字符串的 XML 元素,并将其传递给 XSLT 转换器引擎。
当我收到转换错误时,我想知道 Java 代码如何在将字符串插入最终的 XML 之前处理该字符串,以便 XSLT 转换不会失败。
我目前在 Java 中拥有的是这样的:
String inputValue = ...; // you get this string by an external client
Element target = ...; // element of an XML where you have to add the string
String xml10pattern = "[^"
+ "\u0009\r\n"
+ "\u0020-\uD7FF"
+ "\uE000-\uFFFD"
+ "\ud800\udc00-\udbff\udfff"
+ "]"; // this removes the illegal characters in XML
inputValue = inputValue.replaceAll(xml10pattern, "");
target.setAttribute("text", inputValue);
但是,为了使其更安全,是否还缺少一些东西?
最佳答案
Apache commons 库有 StringEscapeUTils.escapeXML(string)
。这允许在您的属性中包含 &
。
关于java - 在 Java 中安全地准备带有表情符号图标的字符串以进行 XML 和 XSLT 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36883786/