我正在使用JAXB从我的Java类生成XML Schema,以便其他开发人员可以在没有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;
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() {
}
}
@XmlType(name="unit", namespace="http://mygame.com")
@XmlRootElement(name="unit", namespace="http://mygame.com")
public class Unit extends GameObject {
}
@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;
}
@XmlType(name="thing", namespace="http://mygame.com")
public abstract class Thing {
// Constants
// Attributes
@XmlElement(required=false)
public String icon;
}
<?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>
到目前为止,一切都很好。它正确地生成了一个想要的XML(与那些恼人的TNS前缀一样,但这很好)。
问题是当我尝试解组一个UNIT时,我在其中一个案例中遇到错误
鉴于这个XML输入,一切正常,我正确地得到了我的类的实例。
<?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>
鉴于此,我收到了下面描述的错误
<?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>
两个XML输入之间的唯一区别是一个具有元素模型,另一个具有元素tns:model。我不知道为什么我在案例2中收到这个错误,有人可以给我一个解释吗?
答案 0 :(得分:1)
你需要从模型元素中删除tns前缀,或者在模式中指定QUALIFIED的“elementFormDefault”值(个人而言,我更喜欢这个解决方案)。