• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression to replace all invalid xml characters

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic