需要转义java中XML标记(<;、>;、&x27;)之间存在的一些特殊字符

需要转义java中XML标记(<;、>;、&x27;)之间存在的一些特殊字符,java,regex,xml,Java,Regex,Xml,我有一个XML字符串,它已经存在于数据库中,但在解析这个XML字符串时,我遇到了解析问题,因为XML标记之间有特殊字符,如(,') 我使用了名为StringEscapeUtils.escapeXml的API,但它也将转义xml标记。我不想转义xml标记。我只想转义标记值 以下是我的xml字符串:- <start> <attribute name="resourcePageCategory"> <"there 'is' no category"></att

我有一个XML字符串,它已经存在于数据库中,但在解析这个XML字符串时,我遇到了解析问题,因为XML标记之间有特殊字符,如(,')

我使用了名为StringEscapeUtils.escapeXml的API,但它也将转义xml标记。我不想转义xml标记。我只想转义标记值

以下是我的xml字符串:-

<start>
<attribute name="resourcePageCategory"> <"there 'is' no category"></attribute>
<attribute name="resourceType" />
<attribute name="fairMarketValue">1000</attribute>
<attribute name="transferReason" />
<attribute name="effectiveDate" />
<attribute name="amountOwed">10</attribute>
</start>

1000
10
预期输出应如下所示:-

<start>
    <attribute name="resourcePageCategory">  &lt; &quot;there &apos;is&apos; no category&quot;&gt;</attribute>
    <attribute name="resourceType" />
    <attribute name="fairMarketValue">1000</attribute>
    <attribute name="transferReason" />
    <attribute name="effectiveDate" />
    <attribute name="amountOwed">10</attribute>
    </start>

“没有类别”
1000
10
基本上,它应该避开XML标记之间存在的XML特殊字符,因为在我的代码中,我发送这个XML进行解析 请给我任何样本代码来做这件事。 如果我有任何正则表达式模式可以在字符串的replaceAll方法中使用,这是很好的

还请注意,数据以xml字符串形式存储在数据库中。

公共静态字符串修复(字符串xml){
public static String repair(String xml) {
    Pattern pattern = Pattern.compile("(<attribute name=\"[^\"]+\">)(.*?)(</attribute>)");
    Matcher m = pattern.matcher(xml);
    StringBuffer buf = new StringBuffer(xml.length() + xml.length() / 32);
    while (m.find()) {
        String escaped = StringEscapeUtils.escapeXml(m.group(2));
        m.appendReplacement(buf, m.group(1) + escaped + m.group(3));
    }
    m.appendTail(buf);
    return buf.toString();
}

Pattern Pattern=Pattern.compile(“(您的预期输出是什么?到目前为止您尝试了什么?预期输出应该是特殊字符,比如它如何存储在db中?它存储在XML格式Hi Joop中,非常感谢它对我有用。