Hi,
I am new to JAXB and starting to play around it.
But I am facing a problem that is driving me crazy.
I hace looked in the mailing list but not found a direct solution for
"marshalling" instead for "unmarshalling".
The question is the following. I have a schema with the format below, where
some elements are defined with default values. When compiling with XJC
everything is fine and the classes created appear annotated accoridngly as
appear below.
But when trying to marshall a xml document from an object generated based on
the code below, the elements that are not set do not appear in the reulting
XML document.
How can I force that the elements not set appear in the resulting XML
document according to the defualt values and restrictions defined in the
schema?
Thanks and sorry for the basic question !
Best regards,
jose
--------------------XML Schema
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="
http://www.basic_profile.com" xmlns="
http://www.w3.org/2001/XMLSchema" xmlns:basic="
http://www.basic_profile.com"
>
<complexType name="UserProfileType">
<sequence minOccurs="1" maxOccurs="1">
<element name="user_name" minOccurs="1" maxOccurs="1"
type="_basic:NameType" default="NEW_USER"/>
<element name="login_name" type="basic:NameType" default="ali_baba"
minOccurs="1" maxOccurs="1">
</element>
<element name="password" type="basic:PasswordType" default="12345678"
minOccurs="1" maxOccurs="1">
</element>
<element name="group" type="basic:GroupType" default="resident_user"
minOccurs="1" maxOccurs="1">
</element>
<element name="category" type="basic:CategoryType" default="kid" minOccurs="1"
maxOccurs="1">
</element>
</sequence>
</complexType>
<simpleType name="NameType">
<restriction base="string">
<minLength value="3"></minLength>
<maxLength value="30"></maxLength>
</restriction>
</simpleType>
<simpleType name="PasswordType">
<restriction base="string">
<length value="8"></length>
</restriction>
</simpleType>
<simpleType name="GroupType">
<restriction base="string">
<enumeration value="resident_user"></enumeration>
<enumeration value="guest_user"></enumeration>
<enumeration value="administrator"></enumeration>
</restriction>
</simpleType>
<simpleType name="CategoryType">
<restriction base="string">
<enumeration value="kid"></enumeration>
<enumeration value="adult"></enumeration>
</restriction>
</simpleType>
<element name="user_profile" type="basic:UserProfileType"></element>
</schema>
-------------------------- Resulting Class for UserProfile type
*
public* *class* UserProfileType {
@XmlElement(name = "user_name", defaultValue = "NEW_USER")
*protected* String userName;
@XmlElement(name = "login_name", defaultValue = "ali_baba")
*protected* String loginName;
@XmlElement(defaultValue = "12345678")
*protected* String password;
@XmlElement(defaultValue = "resident_user")
*protected* GroupType group;
@XmlElement(defaultValue = "kid")
*protected* CategoryType category;
....................Marshalling Code
*
public* *class* Main_Marshall {
*public* *static* *void* main(String[] args) {
*try* {
// create a JAXBContext
JAXBContext jc = JAXBContext.*newInstance*( "com.basic_profile.estia" );
// create an empty User Profyle
UserProfileType up = *new* UserProfileType();
// set the required attributes
up.setUserName("User_Created in Main_Marshall");
//
up.setGroup(GroupType.*GUEST_USER*);
// create an element for marshalling
JAXBElement<UserProfileType> upElement =
(*new*ObjectFactory()).createUserProfile(up);
upElement.setNil(*false*);
// create a Marshaller and marshal to System.out
Marshaller m = jc.createMarshaller();
m.setProperty( Marshaller.*JAXB_FORMATTED_OUTPUT*, Boolean.*TRUE* );
m.marshal( upElement, System.*out* );
} *catch*( JAXBException je ) {
je.printStackTrace();
}
}
------------------resulting xml* (the elements not set do not appear !!?)*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:user_profile xmlns:ns2="
http://www.basic_profile.com">
<user_name>User_Created in Main_Marshall</user_name>
<group>guest_user</group>
</ns2:user_profile>