2012-08-30 19 views
1

好吧,這是交易。我開始喜歡這個XML文件:JAXB和EntityResolver

<?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"> 

而且開始喜歡了XSD:

<?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" /> 

而解組:

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

爲什麼我得到這個異常:[ org.xml.sax.SAXParseException:cvc-elt.1:找不到元素'ns3:rosApplicationDocument'的聲明。]

編輯:

這裏是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; 
    } 

} 

我將它設置爲出廠,如:schemaFactory.setResourceResolver(new MyLSResourceResolver());

這裏是控制檯輸出:

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'.] 

最後同樣的例外..所以問題不在於xsd

回答

1

在XML您有:

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

所以http://ereg.egov.bg/segment/0009-900001應該是模式的默認命名空間,但我在您發佈的示例代碼中沒有看到它聲明。

此外,該模式中的元素聲明RosApplicationDocument而文件rosApplicationDocument(小寫)在

+0

編輯XML模式是缺少幾行...對不起 – dsthetics

+1

您有名稱的另一個問題:第一個字母在模式中是大寫字母,在文檔中是小寫字母。 – tibtof

+0

是啊,我試過,但由於某種原因,它期望小寫... idk爲什麼 – dsthetics

1

當前您在Unmarshaller上設置Schema以啓用模式驗證,並在您正在解組的驗證XMLReader上創建SAXSource。作爲第一步,我會建議簡化你的代碼,只需做到以下幾點:

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

更多信息

+0

失敗,相同的異常 – dsthetics

+0

您可能需要利用上'SchemaFactory'的'setResourceResolver'方法 - HTTP:// docs.oracle.com/javase/6/docs/api/javax/xml/validation/SchemaFactory.html#setResourceResolver(org.w3c.dom.ls.LSResourceResolver)。 –

+1

試圖... – dsthetics