dcsimg
Try to use XSD to validate XML using SAXParser Java API without luck
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ankur_Saxena
Posted On:   Tuesday, July 18, 2006 03:30 PM

I am trying to work with SAXParser to parse an XML document which works just fine, but now I want to be able to pass a XSD to validate the XML while it is being parsed and if there are an error in the XML for it to spit it out. I have used the following code snipets from J2EE book. //create a handler //create a factory instance //set properties of the factory to name space aware and validating to true //get a SAXParser from the factory, pass it the properties: String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema&quo   More>>


I am trying to work with SAXParser to parse an XML document which works just fine, but now I want to be able to pass a XSD to validate the XML while it is being parsed and if there are an error in the XML for it to spit it out.




I have used the following code snipets from J2EE book.



//create a handler


//create a factory instance


//set properties of the factory to name space aware and validating to true


//get a SAXParser from the factory, pass it the properties:


String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";


String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";


String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";



saxP.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);

saxP.setProperty(JAXP_SCHEMA_SOURCE, new File("/home/ankur/DMAXG/CDMA.1.0.co/lemko-gui/DB.xsd"));



// Parse the input

saxP.parse(new File(filename), handler);


And I have the following XSD and XML





  

    

A


  




AND





<?xml version="1.0"?>



targetNamespace="lemko"
xmlns="lemko"
elementFormDefault="qualified">





  


    


     

    

  






  

    

  

  









I dont know what am I doing wrong, I know my XML is correctly formatted and XSD as well, since I checked that with a parser but I have added an Error in the XML where I have changed the attribute of the DB tag to "t" where as "name" is specified as required in XSD.

What do I do to display the error.

Thanks
Ankur    <<Less

Re: Try to use XSD to validate XML using SAXParser Java API without luck

Posted By:   Roseanne_Zhang  
Posted On:   Wednesday, August 23, 2006 01:12 AM

http://bobcat.webappcabaret.net/javachina/faq/xml_java_01.htm#jaxp_sax_Q60


Above is a validate I wrote quite while ago. I hope it will help you some.


Good luck!

Re: Try to use XSD to validate XML using SAXParser Java API without luck

Posted By:   Ankur_Saxena  
Posted On:   Tuesday, July 18, 2006 04:04 PM


Sorry for the trouble..I solved the problem, I was not overriding the error function in the handler causing the XSD errors to be ignored, all i had to do was




public error(..) throws someexception

{

   throw e;

}




and I was done.



Thanks

Re: Try to use XSD to validate XML using SAXParser Java API without luck

Posted By:   Ankur_Saxena  
Posted On:   Tuesday, July 18, 2006 03:32 PM

Even though the XML above reads I meant which should not pass through the XSD since it says DB tag requires a name attribute.

Thanks
About | Sitemap | Contact