Reputation: 884
I am trying to escape XML special characters in a String. The escaping is taken care by a static method as shown below.
public static String escapeXml10(String response) {
return StringEscapeUtils.escapeXml10(response);
}
Now the issue with such an implementation is that, I get a piece of string which may or may not be parsed. which leads to irregular outputs.
for eg:
Now To get a proper response I am planning to introduce a check in the static metod. as follows by using if Condition.
public static String escapeXml10(String response) {
if(response.contains("&") ||
response.contains("<") ||
response.contains(">") ||
response.contains("'") ||
response.contains(""")){
return response;
}else{
return StringEscapeUtils.escapeXml10(response);
}
}
is this a correct way of implementing, if not please suggest?
Upvotes: 1
Views: 797
Reputation: 8783
int
for integer numbers, float
or double
for decimals, String
for texts, etc.
Then, a proper format should be done just at serializing that data. For example, before serializing to XML, nodes and attributes must be properly placed to form a specific data structure, and user data must be escapped to avoid occurrences of special characters within. Conversely, at the time of reading an XML (=parsing), an unescapping must be done (but this is already done by the parser).Conclussion: You shouldn't even care by escapping if you use standard XML parsers (DocumentBuilderFactory, SAXParser, XMLInputFactory) and serializers (TransformerFactory, XMLOutputFactory). Neither should care your client apps.
Upvotes: 1