• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression to replace all invalid xml characters  RSS feed

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a method to take in string (which are to be used as Element names in an xml doc) and replace invalid characters with an underscore.

NOTE: Names in XML can contain English letters (of any case),
numbers (0-9), underscores (_), periods(.) and hyphens (-).
Names cannot begin with a number, period or hyphen.
Names can also contain a colon, but the use of colons outside of
namespaces should be avoided.

Therefore I need a regular expression to replace any character that is not valid in an XML name.

myString.replaceAll("<regex required>", "_");

Thanks in advance
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked into regex at all? It is not all that difficult if you go through a few tutorials. This, for one, is a good place to start:

and the Pattern class in the API basically sums it up.
[ October 11, 2007: Message edited by: Steinar Steinnes ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!