Java - Removing the double quotes in XML attributes

给你一囗甜甜゛ 提交于 2019-12-02 11:28:37

You can use the following regex:

\s+[\w:.-]+="([^"]*(?:"(?!\s+[\w:.-]+="|\s*(?:\/?|\?)>)[^"]*)*)"

See regex demo. It will match any attribute name/value pair capturing the latter into Group 1 that we can change inside a callback.

Here is a Java code demo:

String s =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <resources> <resource> <properties> <property name=\"name\" value=\"retrieveFoo\"/>\n<property name=\"foo\" value=\"Some corrupted String because of \"something\" like that\"/>";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("(\\s+[\\w:.-]+=\")([^\"]*(?:\"(?!\\s+[\\w:.-]+=\"|\\s*(?:/?|\\?)>)[^\"]*)*)\"").matcher(s);
while (m.find()) {
    m.appendReplacement(result, m.group(1) + m.group(2).replace("\"", "&quot;") + "\"");
}
m.appendTail(result);
System.out.println(result.toString());

Output:

<?xml version="1.0" encoding="UTF-8"?> <resources> <resource> <properties> <property name="name" value="retrieveFoo"/> <property name="foo" value="Some corrupted String because of &quot;something&quot; like that"/>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!