简体   繁体   中英

Exception cvc-elt.1: Cannot find the declaration of element 'AsifXml' when trying to unmarshall using JAXB

I keep receiving the following error when attempting to unmarshall an xml document using JAXB. The error reads as follows:

cvc-elt.1: Cannot find the declaration of element 'AsifXml'

Code to unmarshall is this:

    JAXBContext jc = JAXBContext.newInstance("asif_objects");
    Unmarshaller u = jc.createUnmarshaller();
    SchemaFactory sf = SchemaFactory.newInstance(
            javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("ASIF_Schema1.1.6.xsd"));
    u.setSchema(schema);

    AsifXml doc 
        = (AsifXml)u.unmarshal(new FileInputStream("asif_small.xml"));

I believe the relevant part of the xsd files looks like this(omitted opening and closing of tags as I'm not sure how to format them on the website:

?xml version="1.0" encoding="UTF-8"?
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:asif="http://www.website.com/ASIF" 
targetNamespace="http://www.website.com/ASIF" elementFormDefault="qualified"
attributeFormDefault="unqualified" version="1.1.6"

xs:element name="AsifXml"
    xs:complexType

And what I believe to be the relevant XML is here:

AsifXml xmlns:AsifXml="http://www.website.com/ASIF"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.4" 
content="study"

So far I've tried googling for this same error, but it seems most responses have to do with errors in web services which I'm not using. I also thought that maybe I was having problems validating because I am behind a proxy and it was unable to reach the url's given in the schema documents, but upon further research I'm getting the impression that those url's don't actually have to exist to validate the document. And I tried on another network that isn't behind a proxy and received the same error.

Any help with this problem is greatly appreciated.

The sample XML file looks bogus to me. It looks fine at first glance, but it makes no sense:

<AsifXml xmlns:AsifXml="http://www.website.com/ASIF">

This declares a tag called AsifXml , and declares an XML namespace with a prefix also called AsifXml . However, the two have nothing to do with each other. The element itself is left without any namespace, which violates the schema.

Try the following sample instead, it should work:

<AsifXml xmlns="http://www.website.com/ASIF" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         version="1.1.4" 
         content="study">

This sample say that the AsifXml element has the namespace http://www.website.com/ASIF , which is what the schema expects.

That sample could be equivalently encoded as:

<AsifXml:AsifXml xmlns:AsifXml="http://www.website.com/ASIF" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         version="1.1.4" 
         content="study">

It amounts to the same thing, but this version is confusing and verbose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM