[Xerces] implémentation de XMLCatalogResolver
Salut tout le monde!
Ca fait plusieurs jours que je me prend la tête avec le problème suivant:
Je veux parser et valider un fichier XML par rapport à un schéma XML. Le schéma associé doit être trouver via un catalogue. Malgré tout ce que j'ai lu, je n'arrive pas à faire fonctionner tout le bazard :arf:
J'utilise xerces 2j comme parseur Java et je voudrais utiliser XMLCatalogResolver pour trouver les références locals de mes schémas. D'après ce que j'ai lu, cette classe travaille avec le 'target namespace'. Alors voilà ce que j'ai fait:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
// Set the DocumentBuilderFactory system property
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
// Create the dom factory
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
// Set the namespace property
domFactory.setNamespaceAware(true);
// Set the validation property
domFactory.setValidating(true);
domFactory.setIgnoringComments(true);
domFactory.setIgnoringElementContentWhitespace(true);
// Set the schema language property to be used for validation
domFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
domFactory.setFeature("http://xml.org/sax/features/validation", true);
domFactory.setFeature("http://apache.org/xml/features/validation/schema", true);
domFactory.setFeature("http://apache.org/xml/features/validation/schema/element-default",true);
// Create the document builder
DocumentBuilder builder = domFactory.newDocumentBuilder();
XMLCatalogResolver catalog1 = new XMLCatalogResolver();
String[] catList = new String[1];
catList[0] = "schemas\\Catalog.xml";
catalog1.setCatalogList(catList);
System.out.println(catalog1.resolveURI("http://lucent.com/GRIPP/jmsProperties"));
builder.setEntityResolver(catalog1);
// Create and register an error handler with the parser
JMSErrorHandler parserErrorHandler = new JMSErrorHandler();
builder.setErrorHandler(parserErrorHandler);
// Parse the xml file
Document doc = null;
if (xml instanceof File) {
doc = builder.parse((File)xml);
} else if (xml instanceof String) {
doc = builder.parse(new InputSource(new StringReader((String)xml)));
}
if ( parserErrorHandler.validationError == true ) {
// errors occured during the parsing
if (myLogger.isInfoEnabled()) {
myLogger.info(funcName + "XML file is not valid - Got parser exception");
myLogger.info(parserErrorHandler.saxParseException.getMessage());
}
System.exit(1);
} else {
if (myLogger.isInfoEnabled()) {
myLogger.info(funcName + "XML file is valid");
}
} |
J'obtiens:
Code:
1 2 3
|
XML file is not valid - Got parser exception
schema_reference.4: Failed to read schema document 'JMS_properties.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. |
Mon fichier XML:
Code:
1 2 3 4 5 6
|
<JMSProperties xmlns="http://GRIPP/jmsProperties"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://GRIPP/jmsProperties JMS_properties.xsd">
....
</JMSProperties> |
Mon schéma XML;
Code:
1 2 3 4 5
|
<xs:schema xmlns="http://GRIPP/jmsProperties" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.0" attributeFormDefault="unqualified"
targetNamespace="http://GRIPP/jmsProperties" >
.....
</xs:schema> |
Mon catalog:
Code:
1 2 3 4 5 6 7
|
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<uri name="http://GRIPP/jmsProperties"
uri="schemas/JMS_properties.xsd"/>"
</catalog> |
J'ai l'impression que le catalogue n'est pas utilisé. Je n'ai pas trouvé comment faire pour tracer les appels aux catalogues. Je ne sais plus quoi essayer donc si une bonne âme voulait bien se pencher sur mon petit problème, ce serait méga cool :)