|
|
@@ -23,6 +23,23 @@ public class WordUtil {
|
|
|
{">", ">"}, // > - greater-than
|
|
|
};
|
|
|
|
|
|
+ /**
|
|
|
+ * 转义XML特殊字符
|
|
|
+ */
|
|
|
+ private static String escapeString(String content) {
|
|
|
+ if (StrUtil.isBlank(content)) {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转义特殊字符,除了标签内容
|
|
|
+ if (!content.startsWith("<") && !content.endsWith(">")) {
|
|
|
+ for (String[] pair : XML_ESCAPE) {
|
|
|
+ content = content.replaceAll(pair[0], pair[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 转义XML特殊字符
|
|
|
*/
|
|
|
@@ -32,13 +49,7 @@ public class WordUtil {
|
|
|
Object value = obj.get(key);
|
|
|
if (value instanceof String) {
|
|
|
String content = (String) value;
|
|
|
- // 转义特殊字符,除了标签内容
|
|
|
- if (StrUtil.isNotBlank(content) && !content.startsWith("<") && !content.endsWith(">")) {
|
|
|
- for (String[] pair : XML_ESCAPE) {
|
|
|
- content = content.replaceAll(pair[0], pair[1]);
|
|
|
- }
|
|
|
- obj.put(key, content);
|
|
|
- }
|
|
|
+ obj.put(key, escapeString(content));
|
|
|
continue;
|
|
|
}
|
|
|
//递归调用map
|
|
|
@@ -50,7 +61,13 @@ public class WordUtil {
|
|
|
if (value instanceof Collection) {
|
|
|
JSONArray array = obj.getJSONArray(key);
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
- escapeXml(array.getJSONObject(i));
|
|
|
+ Object o = array.get(i);
|
|
|
+ if (o instanceof String || o.getClass().isPrimitive()) {
|
|
|
+ String content = String.valueOf(o);
|
|
|
+ array.set(i, escapeString(content));
|
|
|
+ } else {
|
|
|
+ escapeXml(array.getJSONObject(i));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|