为什么我的方法使用String.replace和HashMap不替换字符串?

时间:2022-06-12 16:51:25

I am attempting to write a little class to escape characters in an XML document. I am using xpath to get the nodes of the XML document, and passing each node to my class. However, it is not working. I want to change:

我试图写一个小类来转义XML文档中的字符。我使用xpath来获取XML文档的节点,并将每个节点传递给我的类。但是,它不起作用。我想改变:

"I would like a burger & fries."

to

"I would like a burger & fries."

Here is the code for my 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("\"","&quot;");
        xmlCharactersToBeEscaped.put("'","&apos;");
        xmlCharactersToBeEscaped.put("<","&lt;");
        xmlCharactersToBeEscaped.put(">","&gt;");
        xmlCharactersToBeEscaped.put("&","&amp;");
    }

    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;
        }
    }
}

What is happening is that it is replacing the first special character in the map, the quote, and skipping everything else.

发生的事情是它正在替换地图中的第一个特殊字符,引用,并跳过其他所有内容。

2 个解决方案

#1


Your solution is over complicated.

你的解决方案过于复杂。

Use StringEscapeUtils (Part of the Commons Lang library). This has a built in feature to escape and unescape XML, HTML and much more. Commons lang is very easy to import and the following examples are from the latest stable release (3.4). Previous versions use different methods, look up the Java doc dependant on your version. It's very flexible so you can do a lot more with it than just simple escapes and unescapes.

使用StringEscapeUtils(Commons Lang库的一部分)。它具有内置功能,可以逃避和浏览XML,HTML等等。 Commons lang非常容易导入,以下示例来自最新的稳定版本(3.4)。以前的版本使用不同的方法,根据您的版本查找Java文档。它非常灵活,所以你可以用它做更多的事情,而不仅仅是简单的逃脱和失败。

String convertedString = StringEscapeUtils.escapeXml11(inputString);

If you're using XML 1.0 they also offer the following

如果您使用的是XML 1.0,它们还提供以下内容

String convertedString10 = StringEscapeUtils.escapeXml10(inputString);

Get it here: https://commons.apache.org/proper/commons-lang/

在此处获取:https://commons.apache.org/proper/commons-lang/

Java docs here (3.4): https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html

Java docs(3.4):https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html

#2


Make it simpler (and see comment below):

使其更简单(并在下面看到评论):

xmlCharactersToBeEscaped = new HashMap<String,String>();
xmlCharactersToBeEscaped.put("\"","&quot;");
xmlCharactersToBeEscaped.put("'","&apos;");
xmlCharactersToBeEscaped.put("<","&lt;");
xmlCharactersToBeEscaped.put(">","&gt;");
/* xmlCharactersToBeEscaped.put("&","&amp;"); <-- don't add this to the map */

//...
public String replaceSpecialChars(String node) {
    if (node != null) {
        String newNode = node.replace("&", "&amp;"); 
        for (Map.Entry<String, String> e : xmlCharactersToBeEscaped.entrySet()) {              
             newNode = newNode.replace(e.getKey(), e.getValue());
        }
        return newNode;
    } else {
        return null;
    }
}

or better use StringEscapeUtils for such stuff.

或者更好地使用StringEscapeUtils来做这些事情。

#1


Your solution is over complicated.

你的解决方案过于复杂。

Use StringEscapeUtils (Part of the Commons Lang library). This has a built in feature to escape and unescape XML, HTML and much more. Commons lang is very easy to import and the following examples are from the latest stable release (3.4). Previous versions use different methods, look up the Java doc dependant on your version. It's very flexible so you can do a lot more with it than just simple escapes and unescapes.

使用StringEscapeUtils(Commons Lang库的一部分)。它具有内置功能,可以逃避和浏览XML,HTML等等。 Commons lang非常容易导入,以下示例来自最新的稳定版本(3.4)。以前的版本使用不同的方法,根据您的版本查找Java文档。它非常灵活,所以你可以用它做更多的事情,而不仅仅是简单的逃脱和失败。

String convertedString = StringEscapeUtils.escapeXml11(inputString);

If you're using XML 1.0 they also offer the following

如果您使用的是XML 1.0,它们还提供以下内容

String convertedString10 = StringEscapeUtils.escapeXml10(inputString);

Get it here: https://commons.apache.org/proper/commons-lang/

在此处获取:https://commons.apache.org/proper/commons-lang/

Java docs here (3.4): https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html

Java docs(3.4):https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html

#2


Make it simpler (and see comment below):

使其更简单(并在下面看到评论):

xmlCharactersToBeEscaped = new HashMap<String,String>();
xmlCharactersToBeEscaped.put("\"","&quot;");
xmlCharactersToBeEscaped.put("'","&apos;");
xmlCharactersToBeEscaped.put("<","&lt;");
xmlCharactersToBeEscaped.put(">","&gt;");
/* xmlCharactersToBeEscaped.put("&","&amp;"); <-- don't add this to the map */

//...
public String replaceSpecialChars(String node) {
    if (node != null) {
        String newNode = node.replace("&", "&amp;"); 
        for (Map.Entry<String, String> e : xmlCharactersToBeEscaped.entrySet()) {              
             newNode = newNode.replace(e.getKey(), e.getValue());
        }
        return newNode;
    } else {
        return null;
    }
}

or better use StringEscapeUtils for such stuff.

或者更好地使用StringEscapeUtils来做这些事情。