好的,这是交易。我有一个xml文件,开头如下:
<?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中
发布于 2012-08-30 12:56:00
在xml中有:
xmlns:ns3="http://ereg.egov.bg/segment/0009-900001"因此,http://ereg.egov.bg/segment/0009-900001应该是模式的默认名称空间,但我没有看到它在您发布的示例代码中声明过。
此外,在模式中,元素被声明为RosApplicationDocument,而在文档rosApplicationDocument中(小写)
发布于 2012-08-30 12:45:57
目前,您正在Unmarshaller上设置一个Unmarshaller以启用架构验证,并在正在解编组的验证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;
}获取更多信息
https://stackoverflow.com/questions/12196646
复制相似问题