16,472
社区成员
发帖
与我相关
我的任务
分享
public static String replaceString(String str, String regex, String replacement)
{
if (str == null)
{
return null;
}
int index = -1;
index = str.indexOf(regex);
String strNew = "";
if (index >= 0)
{
while (index >= 0)
{
strNew += str.substring(0, index) + replacement;
str = str.substring(index + regex.length());
index = str.indexOf(regex);
}
strNew += str;
return strNew;
}
return str;
}
public static String encodeXmlString(String xml)
{
if (xml == null)
{
return "";
}
xml = replaceString(xml, "&", "&");
xml = replaceString(xml, "<", "<");
xml = replaceString(xml, ">", ">");
xml = replaceString(xml, "'", "'");
xml = replaceString(xml, "\"", """);
return xml;
}
public static String decodeXmlString(String xmlEscaped)
{
xmlEscaped = replaceString(xmlEscaped, "<", "<");
xmlEscaped = replaceString(xmlEscaped, ">", ">");
xmlEscaped = replaceString(xmlEscaped, "'", "'");
xmlEscaped = replaceString(xmlEscaped, """, "\"");
String ret = replaceString(xmlEscaped, "&", "&");
return ret;
}