this question has answer here:
i have xml string there in database while parsing xml string getting parsing issue because have special characters (<,>,') between xml tags.
i have used api called stringescapeutils.escapexml escape xml tags .i dont want escape xml tags.i want escape tag values.
following xml string:-
<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>
expected output should like:-
<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>
basically should escape xml special charaters present between xml tags because in code sending xml parsing please give me sample code this. if have regular expression pattern can use in replaceall method of string.
and note data stored xml string in database.
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(); }
the .*?
momentarily not allow line breaks, add dotall that, , eager (?
) 2 attributes on same line indeed taken two.
Comments
Post a Comment