Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does JAXB map a class name to an XML element name?
    text
    copied!<p>I am using JAXB (the version included with JDK 6) to marshall objects to XML. The following piece of code yields unexpected results: <pre><code> public class JAXBTest { @XmlRootElement public static class VIPPerson {}</p> <pre><code> public static void main(String[] args) throws JAXBException { StringWriter sw = new StringWriter(); VIPPerson p = new VIPPerson(); JAXB.marshal(p, sw); System.out.println(sw.toString()); } } </code></pre> <p></pre></code> The output from the above is</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;vipPerson/&gt; </code></pre> <p>I am expecting to see the class name mapped to the <code>VIPPerson</code> element rather than <code>vipPerson</code> based on section 8.12.1 in the <a href="http://jcp.org/aboutJava/communityprocess/mrel/jsr222/index2.html" rel="nofollow">JAXB specification</a>, which says </p> <blockquote> <p>class name: a class name is mapped to an XML name by de capitalization using java.beans.Introspector.decapitalize(class name ).</p> </blockquote> <p>The JavaDoc for that <code>decapitalize</code> method says this:</p> <blockquote> <p>Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, <strong>but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone</strong>. Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL". </p> </blockquote> <p>Does the implementation violate the spec or am I misunderstanding something?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload