Java – JAXB schemagen ungrouping error

I am using JAXB to generate XML schema from my java class, so that other developers can easily create class instances without java knowledge

This is the relevant part of the code:

package-info. java

@XmlSchema(xmlns = @XmlNs(prefix = "p",namespaceURI = "http://mygame.com"),namespace = "http://mygame.com")

package com.mygame.entity.properties;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;

Model. class

package com.mygame.entity.properties;

@XmlType(name = "model",namespace = "http://mygame.com")
@XmlRootElement(name = "model",namespace = "http://mygame.com")
public class Model {

    @XmlAttribute(required = true)
    public String path;

    public Model() {
    }
}

Unit. class

@XmlType(name="unit",namespace="http://mygame.com")
@XmlRootElement(name="unit",namespace="http://mygame.com")
public class Unit extends GameObject {
}

GameObject. class

@XmlType(name = "gameobject",namespace = "http://mygame.com")
public abstract class GameObject extends Thing {

    // Attributes
    public Armor armor;
    public Short maxHp;
    public Boolean walkable = false;
    public AbstractModel model;
}

Thing. class

@XmlType(name="thing",namespace="http://mygame.com")
public abstract class Thing {
    // Constants
    // Attributes

    @XmlElement(required=false)
    public String icon;
}

Generated XML schema

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://mygame.com" xmlns:e="http://mygame.com" xmlns:s="http://mygame.com" xmlns:tns="http://mygame.com" xmlns:p="http://mygame.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">


  <xs:element name="model" type="tns:model"/>

  <xs:element name="unit" type="tns:unit"/>

  <xs:complexType name="thing" abstract="true">
    <xs:sequence>
      <xs:element name="icon" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="unit">
    <xs:complexContent>
      <xs:extension base="tns:gameobject">
        <xs:sequence/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="gameobject" abstract="true">
    <xs:complexContent>
      <xs:extension base="tns:thing">
        <xs:sequence>
          <xs:element ref="tns:armor" minOccurs="0"/>
          <xs:element name="maxHp" type="xs:short" minOccurs="0"/>
          <xs:element name="walkable" type="xs:boolean" minOccurs="0"/>
          <xs:element ref="tns:model" minOccurs="0"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="model">
    <xs:sequence/>
    <xs:attribute name="path" type="xs:string" use="required"/>
  </xs:complexType>


</xs:schema>

So far, everything has been fine It correctly generates a useful XML (like those annoying TNS prefixes, but that's good)

The problem is that when I try to ungroup the unit, I encounter an error in one of the cases

Case 1 – work

Given this XML input, everything is fine, and I got an instance of my class correctly

<?xml version="1.0" encoding="UTF-8"?><tns:unit xmlns:tns="http://mygame.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/home/shirkit/jMonkeyProjects/Fortress Wars/Core/schema/full.xsd">
<maxHp>100</maxHp>
<walkable>false</walkable>
<model path="Models/Oto/Oto.mesh.xml"/></tns:unit>

Case 2 – not working

In view of this, I get the error described below

<?xml version="1.0" encoding="UTF-8"?><tns:unit xmlns:tns="http://mygame.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/home/shirkit/jMonkeyProjects/Fortress Wars/Core/schema/full.xsd">
<maxHp>100</maxHp>
<walkable>false</walkable>
<tns:model path="Models/Oto/Oto.mesh.xml"/></tns:unit>


unexpected element (uri:"http://mygame.com",local:"model"). Expected elements are <{}icon>,<{}model>,<{}walkable>,<{}armor>,<{}maxHp>

The only difference between the two XML inputs is that one has the element model and the other has the element TNS: model I don't know why I received this error in case 2. Can anyone give me an explanation?

Solution

You need to remove the TNS prefix from the model elements or specify the "elementformdefault" value of qualified in the schema (personally, I prefer this solution)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>