JAXB and EntityResolver

2.8k views Asked by At

Ok so here is the deal. I have an xml file starting like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:rosApplicationDocument xmlns:ns2="http://ereg.egov.bg/segment/0009-000013" xmlns:ns3="http://ereg.egov.bg/segment/0009-900001" xmlns:ns4="http://ereg.egov.bg/segment/0009-000022">

And the xsd starting like that:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://ereg.egov.bg/segment/0009-900001" xmlns="http://ereg.egov.bg/segment/0009-900001"

xmlns:dtn="http://ereg.egov.bg/value/0008-000007" xmlns:emad="http://ereg.egov.bg/value/0008-000036"
xmlns:aisuri="http://ereg.egov.bg/value/0008-000039" xmlns:ssu="http://ereg.egov.bg/value/0008-000077"


xmlns:dtu="http://ereg.egov.bg/segment/0009-000003" xmlns:ebd="http://ereg.egov.bg/segment/0009-000013"
xmlns:rou="http://ereg.egov.bg/segment/0009-000022" xmlns:idu="http://ereg.egov.bg/segment/0009-000046"
xmlns:eovau="http://ereg.egov.bg/segment/0009-000051" xmlns:easu="http://ereg.egov.bg/segment/0009-000091"
xmlns:ss="http://www.bulsi.bg/egov/ServiceSupplier" elementFormDefault="qualified">

<xsd:import namespace="http://www.bulsi.bg/egov/ServiceSupplier"
    schemaLocation="ServiceSupplierType.xsd" />

<xsd:import namespace="http://ereg.egov.bg/value/0008-000007"
    schemaLocation="DocumentTypeName-0008-000007.xsd" />
<xsd:import namespace="http://ereg.egov.bg/value/0008-000036"
    schemaLocation="EmailAddress-0008-000036.xsd" />
<xsd:import namespace="http://ereg.egov.bg/value/0008-000039"
    schemaLocation="AISURI-0008-000039.xsd" />
<xsd:import namespace="http://ereg.egov.bg/value/0008-000077"
    schemaLocation="SUNAUServiceURI-0008-000077.xsd" />

<xsd:import namespace="http://ereg.egov.bg/segment/0009-000003"
    schemaLocation="DocumentTypeURI-0009-000003.xsd" />
<xsd:import namespace="http://ereg.egov.bg/segment/0009-000013"
    schemaLocation="EntityBasicData-0009-000013.xsd" />
<xsd:import namespace="http://ereg.egov.bg/segment/0009-000022"
    schemaLocation="RegisterObjectURI-0009-000022.xsd" />
<xsd:import namespace="http://ereg.egov.bg/segment/0009-000046"
    schemaLocation="InitiatingDocumentURI-0009-000046.xsd" />
<xsd:import namespace="http://ereg.egov.bg/segment/0009-000051"
    schemaLocation="EditorOrVisualizerApplicationURI-0009-000022.xsd" />
<xsd:import namespace="http://ereg.egov.bg/segment/0009-000091"
    schemaLocation="ElectronicAdministrativeServiceURI-0009-000091.xsd" />

<!-- <xsd:key name="serviceID"> <xsd:selector xpath="RosApplicationDocument/Enclosures/SimpleServiceEnclosure"/> 
    <xsd:field xpath="@id"/> </xsd:key> <xsd:keyref name="serviceIDREF" refer="serviceID"> 
    <xsd:selector xpath="RosApplicationDocument/Enclosures/ComplexServiceEnclosure/InitialServices/InitialService/InitialService" 
    /> <xsd:field xpath="@ref"/> </xsd:keyref> -->

<xsd:element name="RosApplicationDocument" type="RosApplicationDocument" />

And the unmarshalling :

JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller um = context.createUnmarshaller();
            if (c.equals(RosApplicationDocument.class)) {
                SchemaFactory schemaFactory = SchemaFactory
                        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = schemaFactory.newSchema(new File(pathToXSD));
                um.setSchema(schema);
                RosApplicationDocument document = (RosApplicationDocument) um
                        .unmarshal(getSource(pathToFile));
                RosApplicationDocumentFactory.removeCDATAFromSegments(document);
                return document;
            }

public static SAXSource getSource(String pathToFile) {
        try {
            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
            parserFactory.setNamespaceAware(true);
            parserFactory.setValidating(true);
            SAXParser saxParser = parserFactory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();
            xmlReader
                    .setEntityResolver(new RosApplicationDocumentEntityResolver());
            InputSource inSrc = new InputSource(new FileReader(pathToFile));
            return new SAXSource(xmlReader, inSrc);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        return null;
    }

Why do i get this exception : [org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns3:rosApplicationDocument'.]

Edit:

Here is the LSResourceResolver:

public class MyLSResourceResolver implements LSResourceResolver {

    @Override
    public LSInput resolveResource(String type, String namespaceURI,
            String publicId, String systemId, String baseURI) {
        System.out.println(publicId);
        System.out.println(systemId);
        System.out.println(baseURI);
        System.out.println(namespaceURI);
        System.out.println(type);
        return null;
    }

}

i set it to the factory like : schemaFactory.setResourceResolver(new MyLSResourceResolver());

And here is the output of the console :

null
ServiceSupplierType.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://www.bulsi.bg/egov/ServiceSupplier
http://www.w3.org/2001/XMLSchema
null
EmailAddress-0008-000036.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/ServiceSupplierType.xsd
http://ereg.egov.bg/value/0008-000036
http://www.w3.org/2001/XMLSchema
null
EntityBasicData-0009-000013.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/ServiceSupplierType.xsd
http://ereg.egov.bg/segment/0009-000013
http://www.w3.org/2001/XMLSchema
null
EntityName-0008-000029.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/EntityBasicData-0009-000013.xsd
http://ereg.egov.bg/value/0008-000029
http://www.w3.org/2001/XMLSchema
null
EntityIdentifier-0008-000028.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/EntityBasicData-0009-000013.xsd
http://ereg.egov.bg/value/0008-000028
http://www.w3.org/2001/XMLSchema
null
DocumentTypeName-0008-000007.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/value/0008-000007
http://www.w3.org/2001/XMLSchema
null
AISURI-0008-000039.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/value/0008-000039
http://www.w3.org/2001/XMLSchema
null
SUNAUServiceURI-0008-000077.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/value/0008-000077
http://www.w3.org/2001/XMLSchema
null
DocumentTypeURI-0009-000003.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/segment/0009-000003
http://www.w3.org/2001/XMLSchema
null
RegisterObjectURI-0009-000022.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/DocumentTypeURI-0009-000003.xsd
http://ereg.egov.bg/segment/0009-000022
http://www.w3.org/2001/XMLSchema
null
BatchNumber-0008-000001.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/RegisterObjectURI-0009-000022.xsd
http://ereg.egov.bg/value/0008-000001
http://www.w3.org/2001/XMLSchema
null
InitiatingDocumentURI-0009-000046.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/segment/0009-000046
http://www.w3.org/2001/XMLSchema
null
DocumentURI-0009-000001.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/InitiatingDocumentURI-0009-000046.xsd
http://ereg.egov.bg/segment/0009-000001
http://www.w3.org/2001/XMLSchema
null
RegisterIndex-0008-000002.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/DocumentURI-0009-000001.xsd
http://ereg.egov.bg/value/0008-000002
http://www.w3.org/2001/XMLSchema
null
DocumentSequenceNumber-0008-000003.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/DocumentURI-0009-000001.xsd
http://ereg.egov.bg/value/0008-000003
http://www.w3.org/2001/XMLSchema
null
DocumentReceiptOrSigningDate-0008-000004.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/DocumentURI-0009-000001.xsd
http://ereg.egov.bg/value/0008-000004
http://www.w3.org/2001/XMLSchema
null
EditorOrVisualizerApplicationURI-0009-000022.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/segment/0009-000051
http://www.w3.org/2001/XMLSchema
null
ElectronicAdministrativeServiceURI-0009-000091.xsd
file:/home/doncho/Documents/data/java/ROSAppl/src/bg/bulsi/egov/rosappl/xml_schemas/appl_v3.xsd
http://ereg.egov.bg/segment/0009-000091
http://www.w3.org/2001/XMLSchema
javax.xml.bind.MarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns3:rosApplicationDocument'.]

The same exception at the end.. So the problem isnt in the xsd

2

There are 2 answers

4
tibtof On BEST ANSWER

In the xml you have:

xmlns:ns3="http://ereg.egov.bg/segment/0009-900001"

So http://ereg.egov.bg/segment/0009-900001 should be the default namespace of your schema, but I don't see it declared in the sample code that you posted.

Also, in the schema the element is declared RosApplicationDocument while in the document rosApplicationDocument (lower case)

1
bdoughan On

Currently you are setting an Schema on the Unmarshaller to enable schema validation, and creating a SAXSource on a validation XMLReader that you are unmarshalling. As a first step I would recommend simplifying your code to simply do the following:

        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller um = context.createUnmarshaller();
        if (c.equals(RosApplicationDocument.class)) {
            SchemaFactory schemaFactory = SchemaFactory
                    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = schemaFactory.newSchema(new File(pathToXSD));
            um.setSchema(schema);
            InputSource inSrc = new InputSource(new FileReader(pathToFile));
            RosApplicationDocument document = (RosApplicationDocument) um
                    .unmarshal(inSrc);
            RosApplicationDocumentFactory.removeCDATAFromSegments(document);
            return document;
        }

For More Information