2013-10-15 117 views
5

我试图验证具有给定xsd的对象时出现问题。 这些类是从xsd生成的。JAXB - SAXParseException找不到元素的声明

SchemaFactory factory = SchemaFactory 
       .newInstance("http://www.w3.org/2001/XMLSchema"); 
Schema schema = factory.newSchema(getClass().getResource("/xsd/test.xsd")); 
JAXBContext context = JAXBContext.newInstance(aClass); 
Unmarshaller u = context.createUnmarshaller(); 
u.setSchema(schema); 
Object anObject = u.unmarshal(new StreamSource(new StringReader(
       MESSAGE)), aClass); 

这里是异常消息

[org.xml.sax.SAXParseException:CVC-elt.1:找不到元素的声明 'ACCESREFUSE'。]

这里是XSD :

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:include schemaLocation="./include/CJCommon.xsd"/> 
<xs:element name="ACCESREFUSE"> 
<xs:complexType> 
    <xs:sequence maxOccurs="1" minOccurs="1"> 
    <!-- Entete --> 
    <xs:element maxOccurs="1" minOccurs="1" ref="IDOper"/> 
    <xs:element maxOccurs="1" minOccurs="1" ref="DateEvt"/> 
    <xs:element maxOccurs="1" minOccurs="1" ref="IDEvt"/> 
    <xs:element maxOccurs="1" minOccurs="1" ref="IDJoueur"/> 
    <xs:element maxOccurs="1" minOccurs="1" ref="HashJoueur"/> 
    <xs:element maxOccurs="1" minOccurs="1" ref="IDSession"/> 
    <xs:element maxOccurs="1" minOccurs="1" ref="IPJoueur"/> 
    <xs:element maxOccurs="1" minOccurs="0" ref="IDCoffre"/> 
    <!-- Corps --> 
    <xs:element maxOccurs="1" minOccurs="1" ref="TypAg"/> 
    <xs:element maxOccurs="1" minOccurs="0" name="CauseRefus" type="string-1024"/> 
    <xs:element maxOccurs="1" minOccurs="0" name="TypeRefus"> 
     <xs:simpleType> 
     <xs:restriction base="string-1024"> 
      <xs:enumeration value="DelaiIdentite"/> 
      <xs:enumeration value="RejetIdentite"/> 
      <xs:enumeration value="Interdit"/> 
      <xs:enumeration value="AutoInterdit"/> 
      <xs:enumeration value="OpVerrouille"/> 
      <xs:enumeration value="Verrouille"/> 
      <xs:enumeration value="Cloture"/> 
      <xs:enumeration value="Autre"/> 
     </xs:restriction> 
     </xs:simpleType> 
    </xs:element> 
    </xs:sequence> 
</xs:complexType> 
</xs:element> 

元素ACCESREFUSE是xml的根。

XML:

<ACCESREFUSE> 
     <dateEvt>Tue Oct 15 11:45:48 CEST 2013</dateEvt> 
     <hashJoueur>0000000000000000000000000000000000000000</hashJoueur> 
     <typAg>JC</typAg> 
     <causeRefus>Interdit</causeRefus> 
     <typeRefus>Interdiction Temporaire</typeRefus> 
     <idjoueur>81.252.190.129</idjoueur> 
     <idoper>002</idoper> 
     <idsession>301090</idsession> 
     <idevt>0</idevt> 
     <ipjoueur/> 
     <idcoffre/> 
</ACCESREFUSE> 

任何想法? 谢谢

回答

4

你可以尝试修改您的架构:

<xs:element name="ACCESREFUSE"> 
<xs:complexType name="ACCESREFUSE"> 
... 
</xs:schema> 

编辑: 我觉得你有与负载XSD问题。您可否更改此代码以进行测试:

InputStream xmlStream = ... 
Schema schema = factory.newSchema(xmlStream); 

请试试!

编辑2:我试着解析ACCESREFUSE类与您的XSD。我不知道你的./include/CJCommon.xsd模式,所以我省略了。这里是我的代码:

 SchemaFactory factory = SchemaFactory 
       .newInstance("http://www.w3.org/2001/XMLSchema"); 
     File file = new File("test.xml"); 
     Schema schema = factory.newSchema(file); 

    JAXBContext context = JAXBContext.newInstance(ACCESREFUSE.class); 
    Unmarshaller u = context.createUnmarshaller(); 
    u.setSchema(schema); 
    Object anObject = u.unmarshal(new StreamSource(new StringReader(
      getMessage())), ACCESREFUSE.class); 

的我ACCESREFUSE.class:

@XmlRootElement(name="ACCESREFUSE") 
public class ACCESREFUSE { 

    protected String v1; 
    protected String v2; 
    protected String v3; 
    protected String v4; 
    protected String v5; 
    protected String v6; 
    protected String v7; 
     protected String v8; 
    protected String v9; 
    protected String CauseRefus; 
    protected TypeRefus typeRefus; 

    public enum TypeRefus{ 
     DelaiIdentite, RejetIdentite, Interdit, AutoInterdit, OpVerrouille, Verrouille, Cloture, Autre; 
    } 

消息:

private static String getMessage() { 

     return "<ACCESREFUSE>" 
       + "<v1>Tue Oct 15 11:45:48 CEST 2013</v1>" 
       + "<v2>0000000000000000000000000000000000000000</v2>" 
       + "<v3>JC</v3>" + "<v4>Interdit</v4>" 
       + "<v5>81.252.190.129</v5>" 
       + "<v6>002</v6>" + "<v7>301090</v7>" 
       + "<v8>0</v8>" + "<v9> test </v9>" 
       + "<TypeRefus>RejetIdentite</TypeRefus>" 

       + "</ACCESREFUSE>"; 
    } 

和修改您的XSD简单的字符串元素:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="ACCESREFUSE"> 
<xs:complexType> 
    <xs:sequence maxOccurs="1" minOccurs="1"> 
    <!-- Entete --> 
    <xs:element maxOccurs="1" minOccurs="1" type="xs:string" name="v1"/> 
    ... 

程序正常工作! :)

所以你可以检查你的./include/CJCommon.xsd?您是否使用xs前缀type="xs:string"

+0

嗯,这很奇怪,因为验证之前工作的错误。只有当一切正常时,我才会在根元素上出现错误。我用InputStream尝试过,但在异常之前结果是 – BkSouX

+0

,在XML不正确时验证正常。例如它告诉我改变字段... – BkSouX

+1

是的,它的工作表示感谢。问题出在CJCommon :)谢谢你的帮助! – BkSouX

0

可能是命名空间问题。你应该尝试导入./include/CJCommon.xsd与适当的命名空间声明等link

+0

我无法更改xsd,但我会尝试。 – BkSouX

+0

nothings changed – BkSouX