XML dynamic validation with Java -


infrastructure: i'm using java 1.5 , mandatory. can load external lib no problem.

problem:

i have xml file recived via "an external channel" , can use inputstream

if need same, use:

inputstream = new fileinputstream(file); 

i need validate xml against xsd has neasted xsd

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"             xmlns:body="urn:cbi:xsd:cbibdysddreq.00.00.06"             xmlns:htrt="urn:cbi:xsd:cbihdrtrt.001.07"             xmlns:he2e="urn:cbi:xsd:cbihdrsrv.001.07"             xmlns:sgnt="urn:cbi:xsd:cbisgninf.001.04"             xmlns:lmsg="urn:cbi:xsd:cbisddreqlogmsg.00.00.06"            xmlns="urn:cbi:xsd:cbisddreqphymsg.00.00.06"            targetnamespace="urn:cbi:xsd:cbisddreqphymsg.00.00.06"            elementformdefault="qualified">     <xs:import namespace="urn:cbi:xsd:cbihdrtrt.001.07" schemalocation="cbihdrtrt.001.07.xsd"/>     <xs:import namespace="urn:cbi:xsd:cbihdrsrv.001.07" schemalocation="cbihdrsrv.001.07.xsd"/>     <xs:import namespace="urn:cbi:xsd:cbibdysddreq.00.00.06" schemalocation="cbibdysddreq.00.00.06.xsd"/>     <xs:element name="cbisddreqphymsg" type="cbisddreqphymsg.00.00.06">         <xs:annotation>             <xs:documentation>1. - tag root dell'intero messaggio fisico di richiesta sdd cbi</xs:documentation>         </xs:annotation>     </xs:element>     <xs:complextype name="cbisddreqphymsg.00.00.06">         <xs:sequence>             <xs:element name="cbihdrtrt" type="htrt:cbihdrtrt.001.07">                 <xs:annotation>                     <xs:documentation>1.1. - header di tratta cbi</xs:documentation>                 </xs:annotation>             </xs:element>             <xs:element name="cbihdrsrv" type="he2e:cbihdrsrv.001.07">                 <xs:annotation>                     <xs:documentation>1.2. - header di servizio cbi</xs:documentation>                 </xs:annotation>             </xs:element>             <xs:element name="cbibdysddreq" type="body:cbibdysddreq.00.00.06">                 <xs:annotation>                     <xs:documentation>1.3. - body di servizio cbi</xs:documentation>                 </xs:annotation>             </xs:element>         </xs:sequence>     </xs:complextype> </xs:schema> 

so have brunch of xsd file.

a chunk xml file this

<?xml version="1.0" encoding="utf-8"?> <msg:cbisddreqphymsg xmlns:msg="urn:cbi:xsd:cbisddreqphymsg.00.00.06"     xmlns:htrt="urn:cbi:xsd:cbihdrtrt.001.07" xmlns:he2e="urn:cbi:xsd:cbihdrsrv.001.07"     xmlns:body="urn:cbi:xsd:cbibdysddreq.00.00.06" xmlns:lmsg="urn:cbi:xsd:cbisddreqlogmsg.00.00.06"     xmlns:sgnt="urn:cbi:xsd:cbisgninf.001.04" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">     <msg:cbihdrtrt>         <htrt:idcbisndrf>0000636j</htrt:idcbisndrf>         <htrt:idcbircvrf>0000641v</htrt:idcbircvrf>         <htrt:srvnm>inc-sddc</htrt:srvnm>         <htrt:idmsgtrt>             0000636jp12312111154007381042010010000636j000000636j0000641v0         </htrt:idmsgtrt> 

so need validate xml against cbisddreqphymsg.00.00.06. know @ runtime against wich xml use. example file load cbisddreqphymsg.00.00.05

i have 2 major problem

1) need obtain xsd file name xml , xml big 1/2gb (stax/sax nice solution)

2) need load xsd jar because whole app cannot have access file system.

for validation prefer system http://www.edankert.com/validate.html

i read include in answer problem validating xml file using java xsd having include

but using loader can't obtain infos file.

some idea?

i have 2 major problem

1) need obtain file name xml , xml big 1/2gb

2) need load xsd jar because whole app cannot have access file system.

1.) filename not inside xml...you should know filename of xml other means (user input, hardcoded, etc.). if have directory full of xml files, run loop on files. pseudo code:

for(getnextfile) {     if(xmlvalidationwithxsd(nextfile))         //passed validation     else         //failed validation } 

i run scripts move xml files 1 place xml-only folder.

2.) believe streamsource allows this. here's did recently:

            public static boolean xmlvalidationwithxsd(string xmllocation){                 source xmlfile = new streamsource(new file(xmllocation));                   //important:  here need.  multiple xsds (that relate each other) , getresource access form .jar files                 //treat xsd resource found in class path, assume full package name passed in xsdlocation                 source schemafilesource1 = new streamsource(benefitenrollmentrequestfileutil.class.getresource(new_xsd_file_resource1).tostring());                 source schemafilesource2 = new streamsource(benefitenrollmentrequestfileutil.class.getresource(new_xsd_file_resource2).tostring());                 source[] schemafilesources = {schemafilesource1, schemafilesource2};                     schemafactory schemafactory = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri);                  try {                     schema schema = schemafactory.newschema(schemafilesources);                     validator validator = schema.newvalidator();                     validator.validate(xmlfile);                     return true;                 } catch (saxexception e) {                     logger.debug(xmlfile.getsystemid() + " not valid", e);                     logger.debug("reason: " + e.getlocalizedmessage());                 } catch (ioexception e) {                     logger.debug(xmlfile.getsystemid() + " not valid", e);                     logger.debug("reason: " + e.getlocalizedmessage());                                  }                 return false;             } 

sources:

referencing xsd schema within jar file

problem validating xml file using java xsd having include

edit:

the relation between file list contained in first tag have list of xsd use , first xsd 1 has same namesace first tag

<msg:cbisddreqphymsg xmlns:msg="urn:cbi:xsd:cbisddreqphymsg.00.00.06" xmlns:htrt="urn:cbi:xsd:cbihdrtrt.001.07" xmlns:he2e="urn:cbi:xsd:cbihdrsrv.001.07" xmlns:body="urn:cbi:xsd:cbibdysddreq.00.00.06" xmlns:lmsg="urn:cbi:xsd:cbisddreqlogmsg.00.00.06" xmlns:sgnt="urn:cbi:xsd:cbisgninf.001.04" xmlns:xsi="w3.org/2001/xmlschema-instance">; 

can't read first line in, extract data? use filereader/dom (it shouldn't slow if you're reading 1 line) retrieve 1 line. parse through string. create arraylist , add each individual piece extract.

check out far efficiency goes:

https://stackoverflow.com/a/2134533/2498729

https://stackoverflow.com/a/12273296/2498729


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -