为什么我的方法使用 String.replace 和 HashMap 不替换字符串?
Why is my method using String.replace and a HashMap not replacing the strings?
我正在尝试编写一些 class 来转义 XML 文档中的字符。我正在使用 xpath 获取 XML 文档的节点,并将每个节点传递给我的 class。但是,它不起作用。我要更改:
"I would like a burger & fries."
到
"I would like a burger & fries."
这是我的 class:
的代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MyReplace{
private static final HashMap<String,String> xmlCharactersToBeEscaped;
private Iterator iterator;
private String newNode;
private String mapKey;
private String mapValue;
static {
xmlCharactersToBeEscaped = new HashMap<String,String>();
xmlCharactersToBeEscaped.put("\"",""");
xmlCharactersToBeEscaped.put("'","'");
xmlCharactersToBeEscaped.put("<","<");
xmlCharactersToBeEscaped.put(">",">");
xmlCharactersToBeEscaped.put("&","&");
}
public String replaceSpecialChar(String node){
if(node != null){
newNode = node;
iterator = xmlCharactersToBeEscaped.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry mapEntry = (Map.Entry) iterator.next();
mapKey = mapEntry.getKey().toString();
mapValue = mapEntry.getValue().toString();
if(newNode.contains(mapKey)){
newNode = newNode.replace(mapKey,mapValue);
}
}
return newNode;
} else {
return node;
}
}
}
正在发生的事情是它正在替换地图中的第一个特殊字符,引号,并跳过其他所有字符。
你的解决方案太复杂了。
使用 StringEscapeUtils(Commons Lang 库的一部分)。它有一个内置的功能来转义和反转义 XML、HTML 等等。 Commons lang 非常容易导入,以下示例来自最新的稳定版本 (3.4)。以前的版本使用不同的方法,请根据您的版本查找 Java 文档。它非常灵活,因此您可以用它做更多事情,而不仅仅是简单的转义和取消转义。
String convertedString = StringEscapeUtils.escapeXml11(inputString);
如果您使用的是 XML 1.0,他们还提供以下内容
String convertedString10 = StringEscapeUtils.escapeXml10(inputString);
在此处获取:https://commons.apache.org/proper/commons-lang/
Java 文档在这里 (3.4):https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html
让它更简单(见下面的评论):
xmlCharactersToBeEscaped = new HashMap<String,String>();
xmlCharactersToBeEscaped.put("\"",""");
xmlCharactersToBeEscaped.put("'","'");
xmlCharactersToBeEscaped.put("<","<");
xmlCharactersToBeEscaped.put(">",">");
/* xmlCharactersToBeEscaped.put("&","&"); <-- don't add this to the map */
//...
public String replaceSpecialChars(String node) {
if (node != null) {
String newNode = node.replace("&", "&");
for (Map.Entry<String, String> e : xmlCharactersToBeEscaped.entrySet()) {
newNode = newNode.replace(e.getKey(), e.getValue());
}
return newNode;
} else {
return null;
}
}
或更好地使用 StringEscapeUtils
来处理此类内容。
我正在尝试编写一些 class 来转义 XML 文档中的字符。我正在使用 xpath 获取 XML 文档的节点,并将每个节点传递给我的 class。但是,它不起作用。我要更改:
"I would like a burger & fries."
到
"I would like a burger & fries."
这是我的 class:
的代码import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MyReplace{
private static final HashMap<String,String> xmlCharactersToBeEscaped;
private Iterator iterator;
private String newNode;
private String mapKey;
private String mapValue;
static {
xmlCharactersToBeEscaped = new HashMap<String,String>();
xmlCharactersToBeEscaped.put("\"",""");
xmlCharactersToBeEscaped.put("'","'");
xmlCharactersToBeEscaped.put("<","<");
xmlCharactersToBeEscaped.put(">",">");
xmlCharactersToBeEscaped.put("&","&");
}
public String replaceSpecialChar(String node){
if(node != null){
newNode = node;
iterator = xmlCharactersToBeEscaped.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry mapEntry = (Map.Entry) iterator.next();
mapKey = mapEntry.getKey().toString();
mapValue = mapEntry.getValue().toString();
if(newNode.contains(mapKey)){
newNode = newNode.replace(mapKey,mapValue);
}
}
return newNode;
} else {
return node;
}
}
}
正在发生的事情是它正在替换地图中的第一个特殊字符,引号,并跳过其他所有字符。
你的解决方案太复杂了。
使用 StringEscapeUtils(Commons Lang 库的一部分)。它有一个内置的功能来转义和反转义 XML、HTML 等等。 Commons lang 非常容易导入,以下示例来自最新的稳定版本 (3.4)。以前的版本使用不同的方法,请根据您的版本查找 Java 文档。它非常灵活,因此您可以用它做更多事情,而不仅仅是简单的转义和取消转义。
String convertedString = StringEscapeUtils.escapeXml11(inputString);
如果您使用的是 XML 1.0,他们还提供以下内容
String convertedString10 = StringEscapeUtils.escapeXml10(inputString);
在此处获取:https://commons.apache.org/proper/commons-lang/
Java 文档在这里 (3.4):https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html
让它更简单(见下面的评论):
xmlCharactersToBeEscaped = new HashMap<String,String>();
xmlCharactersToBeEscaped.put("\"",""");
xmlCharactersToBeEscaped.put("'","'");
xmlCharactersToBeEscaped.put("<","<");
xmlCharactersToBeEscaped.put(">",">");
/* xmlCharactersToBeEscaped.put("&","&"); <-- don't add this to the map */
//...
public String replaceSpecialChars(String node) {
if (node != null) {
String newNode = node.replace("&", "&");
for (Map.Entry<String, String> e : xmlCharactersToBeEscaped.entrySet()) {
newNode = newNode.replace(e.getKey(), e.getValue());
}
return newNode;
} else {
return null;
}
}
或更好地使用 StringEscapeUtils
来处理此类内容。