简体   繁体   中英

JAXB xml validation

Within xsd schema I use inheritance from class that is not defined in schema (for erhitectural reason is it in other module/package) defined as:

 <xsd:complexType name="RemoteHost">
   <xsd:annotation>
     <xsd:appinfo>  
       <jaxb:class ref="com.dummy.entities.RemoteHost"></jaxb:class>
     </xsd:appinfo>
   </xsd:annotation>
 </xsd:complexType>

For example, child classes are defined within same xsd document as:

<xsd:complexType name="hostFirstSubType">
    <xsd:complexContent>
        <xsd:extension base="RemoteHost">
        </xsd:extension>        
    </xsd:complexContent>        
</xsd:complexType>

<xsd:complexType name="otherHostType">
    <xsd:complexContent>
        <xsd:extension base="RemoteHost">
            <xsd:attribute name="id" type="xsd:int" />
        </xsd:extension>            
    </xsd:complexContent>        
</xsd:complexType> 

Class that is sublclassed within xsd is defined as:

@SuppressWarnings("restriction")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "GenericRemoteHost",propOrder = {
        "address",
        "username",
        "password",
        "sshPort"
    })
public class RemoteHost {

    @XmlElement(required = true, namespace = "http://com/dummy/entities/RemoteHost")
    protected String  address;
    @XmlElement
    protected String  username;
    @XmlElement
    protected String  password;
    @XmlElement
    protected Integer sshPort;
    ...

Sample part of xml to load:

        <hostFirstSubType>
            <rh:address>127.0.0.1</rh:address>
            <rh:username>user</rh:username>
            <rh:password>pass</rh:password>                             
            <rh:sshPort>22</rh:sshPort>             
        </hostFirstSubType>

        <otherHostType id="1">
            <rh:address>127.0.0.1</rh:address>
            <rh:username>user</rh:username>
            <rh:password>pass</rh:password>                             
            <rh:sshPort>22</rh:sshPort>             
        </otherHostType>    

I created a validation collector to display all validation errors during unmarshalling of xml file against that schema and result is somehow confusing: Validation collector shows validation error for each class inheriting my RemoteHost as:

Element 'hostFirstSubType' must have no character or element information item [children], because the type's content type is empty.

Element 'otherHostType' must have no character or element information item [children], because the type's content type is empty

However, when I skip validation part, xml file is properly loaded into java objects, all data is loaded and accessibile with both getters and setters.

As sugested at some other posts, I tried to change @XmlAccessorType(XmlAccessType.NONE) to other available values but without luck.

Anybody have a clue what else to look for...

The behaviour you are seeing matches what you have:

  1. Your XML contains elements that are not defined in your XML schema therefore you are getting validation errors.
  2. Your XML contains elements that are mapped to your object model, and this is working correctly.

Things you can do:

  1. Include the definition for the XML elements in your document to the XML schema.
  2. Make the definition for your XML elements more flexible leveraging the any concept.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM