AIDE : PARSEUR VALIDANT SCHEMA XML

darktonight Messages postés 51 Date d'inscription mardi 4 mars 2003 Statut Membre Dernière intervention 23 juillet 2004 - 24 mars 2003 à 15:35
arnoo59 Messages postés 10 Date d'inscription mardi 12 octobre 2004 Statut Membre Dernière intervention 8 février 2005 - 3 janv. 2005 à 17:31
Bonjour, je suis sur ce probleme depuis prés de 3 semaines et je n'y parviens toujours pas.

Je dois valider un document xml lié à un schema (xsd) via SAX de SUN.
Je comptais utilisé JAXP qui selon Sun est intégré dans jsdk1.4, donc pas de probleme de classpath et autre.
Mais je n'y parviens pas.
J'ai utilisé un exemple qui devrait marché mais j'aboutis à une erreur de SAXRecognizedException
Voila le code de l'exemple :
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class Schema1{

//Constants when using XML Schema for SAX parsing.
static final String JAXP_SCHEMA_LANGUAGE =
    	"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
    "http://www.w3.org/2001/XMLSchema"; 
static final String JAXP_SCHEMA_SOURCE =
    	"http://java.sun.com/xml/jaxp/properties/schemaSource";

//main
public static void main(String[] args) {		 
String msg,xml,xsd;
if(args.length!=2){
xml = "letter.xml";
xsd = "letter.xsd";
System.out.println("No arguments found:\nUsage: xmlFile xsdFile" + 
"\nUsing letter.xml letter.xsd");
}else{
xml = args[0];
xsd = args[1];
}

  		if(isValidSAX("letter.xml","letter.xsd")){
  			msg = "File is valid";
  		}else{
  			msg = "ERROR! INVALID FILE";
  		}
  		System.out.println(msg);
  	} 
  
 

//SAX parsing
public static boolean isValidSAX(String source, String schema)
{
try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(true);
SAXParser sp = spf.newSAXParser();
sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
sp.setProperty(JAXP_SCHEMA_SOURCE, schema);
MyDefaultHandler dh = new MyDefaultHandler();
sp.parse(source, dh);

return (dh.isValid)?true:false;

    } catch(SAXException se) {
se.printStackTrace();
    	}catch (Exception e) {
  		System.err.println(e);
  	}	  	  
return false;
}

}

//Custom error hanler to print errors and return isValid
class MyDefaultHandler extends DefaultHandler {

//flag to check if the xml document was valid
public boolean isValid = true;

//Receive notification of a recoverable error.
public void error(SAXParseException se) {
setValidity(se);
}

//Receive notification of a non-recoverable error. 
 	public void fatalError(SAXParseException se) {
 		setValidity(se);
    }

//Receive notification of a warning.        
 	public void warning(SAXParseException se) { 
 		setValidity(se);
}

private void setValidity(SAXParseException se){
isValid = false;
    	System.out.println(se.toString());
}
}


Je recherche desesperement un exemple de code pour comprendre comment y parvenir.

Merci.

2 réponses

arnoo59 Messages postés 10 Date d'inscription mardi 12 octobre 2004 Statut Membre Dernière intervention 8 février 2005
3 janv. 2005 à 17:29
0
arnoo59 Messages postés 10 Date d'inscription mardi 12 octobre 2004 Statut Membre Dernière intervention 8 février 2005
3 janv. 2005 à 17:31
As tu trouvés comment faire ????



je suis dans la meme situation...

nono
0
Rejoignez-nous