xml - Java SAX parser validation -


i'm using sax parser custom handler parse xml files. works far, want check more well-formedness of given file , use validation via xsd scheme, contains default values optional attributes. there lots of tutorials online on doing this, not able find way satisfies constraints, follows:

-i don't know scheme beforehand, have bunch of xml , xsd files , every xml contains information xsd should conform to

-the validatior should alter stream handler gets , insert default values optional attributes xsd if necessary

-the current custom handler should used

i'm new topic, can't preclude i've stumbled on solution without beeing aware of it, i'm confused on how this.

here minimum sscce, should show problem , related parts:

package parsertest;  import java.io.file; import java.io.ioexception;  import javax.xml.parsers.parserconfigurationexception; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import javax.xml.validation.typeinfoprovider; import javax.xml.validation.validatorhandler;  import org.w3c.dom.ls.lsresourceresolver; import org.xml.sax.attributes; import org.xml.sax.contenthandler; import org.xml.sax.errorhandler; import org.xml.sax.locator; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler;  public class parsertest {   public final static void main(string[] args)   {     //initialize sax parser     final saxparserfactory saxfactory = saxparserfactory.newinstance();     saxparser saxparser = null;     try     {       saxparser = saxfactory.newsaxparser();     }     catch(parserconfigurationexception confex){confex.printstacktrace();}     catch (saxexception saxex){saxex.printstacktrace();}      //initialize handler     defaulthandler saxhandler = new customhandler();      validatorhandler vh = new validatorhandler()     {       @override       public void startprefixmapping(string prefix, string uri) throws saxexception{}        @override       public void startelement(string uri, string localname, string qname, attributes atts) throws saxexception{}        @override       public void startdocument() throws saxexception{}        @override       public void skippedentity(string name) throws saxexception{}        @override       public void setdocumentlocator(locator locator){}        @override       public void processinginstruction(string target, string data) throws saxexception{}        @override       public void ignorablewhitespace(char[] ch, int start, int length)throws saxexception{}        @override       public void endprefixmapping(string prefix) throws saxexception{}        @override       public void endelement(string uri, string localname, string qname) throws saxexception{}        @override       public void enddocument() throws saxexception{}        @override       public void characters(char[] ch, int start, int length) throws saxexception{}        @override       public void setresourceresolver(lsresourceresolver resourceresolver){}        @override       public void seterrorhandler(errorhandler errorhandler){}        @override       public void setcontenthandler(contenthandler receiver){}        @override       public typeinfoprovider gettypeinfoprovider(){return null;}        @override       public lsresourceresolver getresourceresolver(){return null;}        @override       public errorhandler geterrorhandler(){return null;}        @override       public contenthandler getcontenthandler(){return null;}     };      vh.setcontenthandler(saxhandler);      //do parsing     file input = new file("");     try     {       saxparser.parse(input, saxhandler);       //saxparser.parse(input, vh);       //<-- first attempt, gives me error message       //saxparser.setcontenthandler(vh);  //<-- second attempt, parser not seem know method     }     catch (ioexception ioex){ioex.printstacktrace();}     catch (saxexception saxex){saxex.printstacktrace();}   }    /*    * class handler used class.    */   static private final class customhandler extends defaulthandler   {     //handle start of element     public final void startelement(string namespaceuri, string localname, string qname, attributes atts){}      //handle end of element     public final void endelement(string namespaceuri, string localname, string qname){}      //handle start of characters     public final void characters(char[] ch, int start, int length){}   } } 

the basic principle insert validatorhandler between sax parser , contenthandler

https://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/validation/validatorhandler.html

validatorhandler vh = new validatorhandler(); vh.setcontenthandler(originalcontenthandler); parser.setcontenthandler(vh); 

the tricky bit in order create validatorhandler, need know schema in use. how identified? if uses xsi:schemalocation attribute, can (probably) validatorhandler pick automatically. if uses custom mechanism, may have "prepass" reading (some of) source file discover schema, reading again validatorhandler in place.

your contenthandler notified of default values optional attributes.


Comments