Andrei Ciobanu
Andrei Ciobanu

Reputation: 12848

Checking if a string is valid tag/attribute name for a XML document

Scenario

I need to write a validation function that validates XML tag names (or attribute names) .

Eg.:

If a string is not valid i should escape that makes it invalid, and replace them with some arbitrary character (or remove it) .

Eg.:

Those functions will be heavily called - so I need to take in consideration code effectiveness.

My problem(s)

Thank you!

Upvotes: 2

Views: 2339

Answers (1)

Andreas Dolk
Andreas Dolk

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

Related Questions