English 中文(简体)
JAXB Schemagen and unmarhalling差
原标题:JAXB Schemagen and unmarshalling error
  • 时间:2012-04-29 15:40:42
  •  标签:
  • java
  • xml
  • jaxb

I m using JAXB to generate a XML Schema from my Java classes so the other developers can create instances of the classes easily without knowledge in Java.

Here s 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>

直到这一点为止,一切都是罚款的。 它正确地产生了一种想要的XML(尽管与那些宣布英国国家安全局预设机构,但却是罚款)。

问题在于我试图对一个联合国国际交易日志进行辩驳,在其中一个案件中,我错了。

Case 1 - Working

鉴于这一十大运动的投入,所有东西都行得当,我得到的就是我的班子。

<?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

有鉴于此,我犯了以下错误:

<?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投入的唯一区别是,一种具有要素模式,另一种具有要素标记:模型。 我不知道为什么我在案件2中看到这一错误,有人会给我解释?

最佳回答

你们要么需要从模型要素中删除斜线,要么在你的图谋中说明“变形防御”的数值(从个人角度来说,更喜欢这一解决办法)。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签