java - Restrict tag insertion in XML -


my question related java. have xml valid per xsd, have used dom parser , got document object out of (i using jaxp). now, want disallow insertion of invalid xml element. so, when try doing appendchild() should exception if element invalid per schema.

i have set schema @ factory level before getting dom. however, seems fine insertion of invalid elements. know document incorrect when using validation api check validity of document happening @ end.

here xml

<?xml version="1.0" encoding="utf-8"?>  <company xmlns="uri:trial">   <business>     <apac>tns:apac</apac>   </business>      <nonbusiness>     <facilities>tns:facilities</facilities>   </nonbusiness> </company> 

the xsd

<?xml version="1.0" encoding="utf-8"?> <schema xmlns="http://www.w3.org/2001/xmlschema" targetnamespace="uri:trial"     xmlns:tns="uri:trial" elementformdefault="qualified"     attributeformdefault="qualified">      <element name="company" type="tns:mycomplextype"></element>       <complextype name="mycomplextype">         <sequence>             <element ref="tns:business"></element>             <element ref="tns:nonbusiness"></element>         </sequence>     </complextype>      <element name="business" type="tns:businesstype"></element>         <element name="apac" type="string"></element>      <element name="aus" type="string"></element>      <element name="nonbusiness" type="tns:nonbusinesstype"></element>      <element name="facilities" type="string"></element>        <complextype name="businesstype">             <choice>             <element ref="tns:apac"></element>             <element ref="tns:aus"></element>         </choice>     </complextype>      <complextype name="nonbusinesstype">         <sequence>             <element ref="tns:facilities"></element>         </sequence>     </complextype> </schema> 

the java

public class mainjaxpvalidation {

public static void main(string[] args) throws exception {      file fxml = new file ("xmls/trial.xml");     file fxsd = new file ("xmls/trial.xsd");        schemafactory sf = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri);     schema schema = sf.newschema(new streamsource(fxsd));      documentbuilderfactory dbf = documentbuilderfactory.newinstance();     dbf.setschema(schema);     dbf.setnamespaceaware(true); //  dbf.setvalidating(true);     documentbuilder db = dbf.newdocumentbuilder();      document doc = db.parse(fxml);      node tempnode = doc.createelement("somechild");      doc.appendchild(tempnode);      domsource ds = new domsource(doc);      validator validator = schema.newvalidator();      validator.validate(ds);  }  } 

can suggest alternative... have custom class say, mydocument extends documentimpl. in class, override appendchild , other necessary methods. in overridden method, can validation , throw runtime exceptions.

  import com.sun.org.apache.xerces.internal.dom.documentimpl; public class mydocument extends documentimpl{      document document;     schema schema;      public mydocument(file xmlfile,file schemafile) throws saxexception, ioexception, parserconfigurationexception {         this.document = documentbuilderfactory.newinstance().newdocumentbuilder().parse(xmlfile);         schemafactory sf = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri);         this.schema = sf.newschema(new streamsource(schemafile));      }       @override     public node appendchild(node newchild) throws domexception { //validation     return super.appendchild(newchild);     } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -