java - while validation, i need element name in "exception" -


this java class file.

import javax.xml.xmlconstants; import javax.xml.transform.source; import javax.xml.transform.stream.streamsource; import javax.xml.validation.*; import org.xml.sax.saxexception; import java.io.*;  public class jaxp_1 { public static void main(string [] args) throws exception { source schemafile = new streamsource(new file("xsd/img.xsd")); source xmlfile = new streamsource(new file("xml/imgone.xml"));  schemafactory schemafactory = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri); schema schema = schemafactory.newschema(schemafile); validator validator = schema.newvalidator();  try{         validator.validate(xmlfile);         system.out.println(xmlfile.getsystemid()+ " valid");         system.out.println();     }     catch (saxexception e)      {         system.out.println(schemafile.getsystemid() + " not valid");         system.out.println("reason: " + e.getmessage());     }  }  } 

this xml file.

<?xml version="1.0" encoding="utf-8"?> <edge xmlns="http://www.example.org/img"> <image x="143.05" y="2" height="66" width="537"     xhref="/dccp_repository/dam/other/images/insurance.jpg" id="image_48"     islocked="false" rx="143.05" ry="2" rotation="0" /> </edge> 

this xsd file.

<?xml version="1.0" encoding="utf-8"?> <schema xmlns="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.example.org/img" xmlns:tns="http://www.example.org/img" elementformdefault="qualified"> <element name="edge">     <complextype>         <sequence>             <element name="image">                 <complextype>                     <attribute name="x" type="int"></attribute>                     <attribute name="y" type="int"></attribute>                     <attribute name="height" type="int"></attribute>                     <attribute name="width" type="int"></attribute>                     <attribute name="xhref" type="string"></attribute>                     <attribute name="id" type="string"></attribute>                     <attribute name="islocked" type="string"></attribute>                     <attribute name="rx" type="double"></attribute>                     <attribute name="ry" type="int"></attribute>                     <attribute name="rotation" type="int"></attribute>                 </complextype>             </element>         </sequence>     </complextype> </element> </schema> 

i changed type of attribute attribute name="x" type="int". got error :

file:/d:/maheshkumar.v/workspace/javaone/javaio/xsd/img.xsd not valid reason: cvc-datatype-valid.1.2.1: '143.05' not valid value 'integer'.

it won't show element name in exception . integer ? don't specific 'image' element.

if had large xml. how can identify error?

the exception thrown saxparseexception, subclass of saxexception. saxexception cannot tell fault occurred, saxparseexception can, through getlinenumber() , getcolumnnumber(). these not name element @ fault allow identify through location in xml file. line , column number point closing tag element.

you use this:

try{     validator.validate(xmlfile);     system.out.println(xmlfile.getsystemid()+ " valid");     system.out.println(); } catch (saxparseexception e)  {     system.out.println(schemafile.getsystemid() + " not valid");     system.out.println("reason: " + e.getmessage()           + " @ line:" + e.getlinenumber()           + " @ column:" + e.getcolumnnumber() +"."); } catch (saxexception e)  {     system.out.println(schemafile.getsystemid() + " not valid");     system.out.println("reason: " + e.getmessage()); } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -