开发者

How does JAXB map a class name to an XML element name?

开发者 https://www.devze.com 2023-02-12 20:07 出处:网络
I am using JAXB (the version included with JDK 6) to marshall objects to XML. The following piece of code yields unexpected results:

I am using JAXB (the version included with JDK 6) to marshall objects to XML. The following piece of code yields unexpected results:


    public class JAXBTest {
        @XmlRootElement
        public static class VIPPerson {}

    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());
    }
}

The output from the above is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vipPerson/>

I am expecting to see the class name mapped to the VIPPerson element rather than vipPerson based on section 8.12.1 in the JAXB specification, which says

class name: a class name is mapped to an XML name by de capitalization using java.beans.Introspector.decapitalize(class name ).

The JavaDoc for that decapitalize method says this:

Utili开发者_开发技巧ty 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, 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. Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

Does the implementation violate the spec or am I misunderstanding something?


This is caused by a bug in the reference implementation. Looks like it won't be fixed due to the compatibility problems it would cause. The workaround is to explicitly specify a name with @XmlRootElement.

0

精彩评论

暂无评论...
验证码 换一张
取 消