Creating the correct XML name in Java

Are there any helpers that will convert / delete the string as a valid XML name?

For example, I have a string max(OfAll) and you need to generate some XML, for example,

 <max(OfAll)>SomeText</<max(OfAll)> 

This is clearly not a valid name, are there any helper methods that can convert the string to the correct xml name?

(For comparison, .NET has some methods that would be higher: x /

  <max_x028_OfAll_x028_>SomeText</<max_x028_OfAll_x028_>) 
+4
source share
4 answers

The encoding in your .NET example looks like in ISO9075. I don’t think jdk has a built-in implementation, but this encoding is also used by content repositories such as alfresco or jackrabbit to import / export xml and apis request. A quick search revealed these two implementations, both available under open source licenses:

+2
source

One class that can be used in other situations is StringEscapeUtils in the apache commons-lang project. It can avoid text for use in XML documents, I don’t know anything to avoid the names of XML elements.

Could you create something more readable, for example

 <aggregation type="max(OfAll)">SomeText</aggregation> 

There are many libraries available for marshall / unmarshall objects for xml and vice versa, including JAXB (part of the JDK), JiBX , Castor , XStream

0
source

I do not know any helper methods for this, but the rules here http://www.w3.org/TR/REC-xml/#NT-Name are pretty simple, so this should be easy to implement.

0
source

As should be clear, the usual XML escaping (replacing inappropriate characters with character entities) does not result in a valid XML identifier.

For the record, what you do is often called "mangling."

0
source

All Articles