Java JAXB - SAXParseException 找不到元素的声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19380266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:37:11  来源:igfitidea点击:

JAXB - SAXParseException Cannot find the declaration of element

javaxmlparsingxml-parsingjaxb

提问by BkSouX

I have a problem trying to validate an object with a given xsd. The classes has been generated from the xsd.

我在尝试使用给定的 xsd 验证对象时遇到问题。这些类是从 xsd 生成的。

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);

Here is the exception message

这是异常消息

[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ACCESREFUSE'.]

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

Here is the XSD :

这是 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>

The element ACCESREFUSE is the root of the xml.

元素 ACCESREFUSE 是 xml 的根。

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>

Any idea ? Thank you

任何的想法 ?谢谢

采纳答案by herry

Can you try modify your schema to:

您可以尝试将架构修改为:

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

EDIT:I think you have problem with load XSD. Could you change this code for test purpose:

编辑:我认为您在加载 XSD 时遇到问题。您能否更改此代码以进行测试:

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

Please try it!

请尝试一下!

EDIT2:I tried parse ACCESREFUSEclass with your XSD. I don't know your ./include/CJCommon.xsdschema, so I omitted. Here is my code:

EDIT2:我试过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);

The my 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;
    }

The message:

消息:

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>";
    }

and modified your XSD to simple string elements:

并将您的 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"/>
     ...

The program correctly working! :)

程序正常运行!:)

So could you check your ./include/CJCommon.xsd? Are you use xs prefix for type="xs:string"?

所以你能检查一下你的./include/CJCommon.xsd吗?你使用 xs 前缀type="xs:string"吗?

回答by user2880879

May be the namespace issue. you should try import ./include/CJCommon.xsd with proper namespace declaration etc. link

可能是命名空间问题。您应该尝试使用正确的命名空间声明等导入 ./include/CJCommon.xsd链接