Reputation: 12848
Scenario
I need to write a validation function that validates XML tag names (or attribute names) .
Eg.:
"div"
is valid "d<iv"
is not valid"d\iv"
is not validIf a string is not valid i should escape that makes it invalid, and replace them with some arbitrary character (or remove it) .
Eg.:
"d<iv"
is not valid -> I replace it with "div"
.Those functions will be heavily called - so I need to take in consideration code effectiveness.
My problem(s)
Thank you!
Upvotes: 2
Views: 2339
Reputation: 114817
The rules are defined in the xml spec (look at the name definition)
If speed matters, then don't use regular expressions. Do it more like this:
public static String correctName(String name) {
StringBuilder nameBuilder = new StringBuilder();
for (char nameChar:name.charArray())
if (isValidXml(nameChar)) // some magic left to do ;)
nameBuilder.append(nameChar);
return nameBuilder.toString();
}
Note - the code above is a simple guideline, it does not cover the little annoyance, that the first char of an xml name has a different value range ... if you want to correct illegal tags like $%&div
then it's a bit more complicated (more magic needed)
Upvotes: 3