Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB and EntityResolver
    text
    copied!<p>Ok so here is the deal. I have an xml file starting like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;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"&gt; </code></pre> <p>And the xsd starting like that:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;xsd:import namespace="http://www.bulsi.bg/egov/ServiceSupplier" schemaLocation="ServiceSupplierType.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/value/0008-000007" schemaLocation="DocumentTypeName-0008-000007.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/value/0008-000036" schemaLocation="EmailAddress-0008-000036.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/value/0008-000039" schemaLocation="AISURI-0008-000039.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/value/0008-000077" schemaLocation="SUNAUServiceURI-0008-000077.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/segment/0009-000003" schemaLocation="DocumentTypeURI-0009-000003.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/segment/0009-000013" schemaLocation="EntityBasicData-0009-000013.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/segment/0009-000022" schemaLocation="RegisterObjectURI-0009-000022.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/segment/0009-000046" schemaLocation="InitiatingDocumentURI-0009-000046.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/segment/0009-000051" schemaLocation="EditorOrVisualizerApplicationURI-0009-000022.xsd" /&gt; &lt;xsd:import namespace="http://ereg.egov.bg/segment/0009-000091" schemaLocation="ElectronicAdministrativeServiceURI-0009-000091.xsd" /&gt; &lt;!-- &lt;xsd:key name="serviceID"&gt; &lt;xsd:selector xpath="RosApplicationDocument/Enclosures/SimpleServiceEnclosure"/&gt; &lt;xsd:field xpath="@id"/&gt; &lt;/xsd:key&gt; &lt;xsd:keyref name="serviceIDREF" refer="serviceID"&gt; &lt;xsd:selector xpath="RosApplicationDocument/Enclosures/ComplexServiceEnclosure/InitialServices/InitialService/InitialService" /&gt; &lt;xsd:field xpath="@ref"/&gt; &lt;/xsd:keyref&gt; --&gt; &lt;xsd:element name="RosApplicationDocument" type="RosApplicationDocument" /&gt; </code></pre> <p>And the unmarshalling :</p> <pre><code>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; } </code></pre> <p>Why do i get this exception : <strong>[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ns3:rosApplicationDocument'.]</strong></p> <p>Edit:</p> <p>Here is the LSResourceResolver:</p> <pre><code>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; } } </code></pre> <p>i set it to the factory like : <code>schemaFactory.setResourceResolver(new MyLSResourceResolver());</code></p> <p>And here is the output of the console :</p> <pre><code>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'.] </code></pre> <p>The same exception at the end.. So the problem isnt in the xsd</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload