Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

all validation error using saxParser

 
Tapas Mohapatra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am validating an xml using xsd. I am Getting only the first error in the xml file. I want to see all the errors.

below is the code..

public class EDITExtractSchema {

static Document document;

public void validateXMLVOusingXSD(String str)
{

try {
//create a SchemaFactory capable of understanding W3C XML Schemas (WXS)
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
//Set the error handler to receive any error during Schema Compilation
XMLErrorHandler errorHandler = new XMLErrorHandler();
factory.setErrorHandler(errorHandler);

//set the resource resolver to customize resource resolution
//factory.setResourceResolver( new MyLSResourceResolver());
// load a WXS schema, represented by a Schema instance
File schemaFile = new File("C:/Temp/project2-CorrVO/XSDs/TaxExtract5498DetailVO.xsd");
Schema schema = factory.newSchema(new StreamSource(schemaFile));
SAXParserFactory spf = SAXParserFactory.newInstance();
//spf.setNamespaceAware(true);
//spf.setValidating(true);
spf.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);


//Just set the Schema instance on SAXParserFactory
spf.setSchema(schema);

//Obtain the SAXParser instance
SAXParser saxParser = spf.newSAXParser();

//parser will parse the XML document but validate it using Schema instance
//saxParser.parse(new File(str), myHandler);
saxParser.parse(new File(str),new MyDefaulyHandler());
}catch(ParserConfigurationException e) {
System.out.println("ParserConfigurationException-" + e);
}catch (SAXException e) {
System.out.println("SAXException-" + e);
}
catch (Exception e) {
System.out.println("Exception-" + e);
}






/*// output the errors XML
XMLWriter writer = null;
try {
writer = new XMLWriter(OutputFormat.createPrettyPrint());
} catch (UnsupportedEncodingException e) {
System.out.println("validate error" + e);
e.printStackTrace();
}
try {
writer.write(errorHandler.geterrors());
} catch (IOException e) {
System.out.println("validate error" + e);
e.printStackTrace();
}
*/


}

//implement error handler
public static class XMLErrorHandler implements ErrorHandler {

private boolean valid = true;

public void reset() {
// Assume document is valid until proven otherwise
valid = true;
}

public boolean isValid() {
return valid;
}

public void warning(SAXParseException exception) throws SAXException{


System.out.println("Warning: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber()
+ ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
valid = true;

}


public void error(SAXParseException exception) throws SAXException{


System.out.println("Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber()
+ ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
valid = true;
/* // output the errors XML
XMLWriter writer = null;
try {
writer = new XMLWriter(OutputFormat.createPrettyPrint());
} catch (UnsupportedEncodingException e) {
System.out.println("validate error" + e);
e.printStackTrace();
}
try {
writer.write((Object)exception);
} catch (IOException e) {
System.out.println("validate error" + e);
e.printStackTrace();
}
*/

}


public void fatalError(SAXParseException exception) throws SAXException {


System.out.println("Fatal Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber()
+ ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
valid = true;

}
}

public static class MyDefaulyHandler extends DefaultHandler {

private boolean valid = true;

public void reset() {
// Assume document is valid until proven otherwise
valid = true;
}

public boolean isValid() {
return valid;
}

public void warning(SAXParseException exception) throws SAXException{


System.out.println("Warning: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber()
+ ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
valid = true;

}


public void error(SAXParseException exception) throws SAXException{


System.out.println("Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber()
+ ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
valid = true;
/* // output the errors XML
XMLWriter writer = null;
try {
writer = new XMLWriter(OutputFormat.createPrettyPrint());
} catch (UnsupportedEncodingException e) {
System.out.println("validate error" + e);
e.printStackTrace();
}
try {
writer.write((Object)exception);
} catch (IOException e) {
System.out.println("validate error" + e);
e.printStackTrace();
}
*/

}


public void fatalError(SAXParseException exception) throws SAXException {


System.out.println("Fatal Error: " + exception.getMessage());
System.out.println(" at line " + exception.getLineNumber()
+ ", column " + exception.getColumnNumber());
System.out.println(" in entity " + exception.getSystemId());
valid = true;

}
}



public static void main(String[] args) {

EDITExtractSchema schemaClass = new EDITExtractSchema();
try {
schemaClass.validateXMLVOusingXSD("C:/Temp/project2-CorrVO/XSDs/TaxExtract5498DetailVO.xml");
}catch (Exception e) {
System.out.println("xml file read error Exception-" + e);
}
// */

}


}

=======
would appreciate your help.

Thanks,
RabiM
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

You should be able to get any number of warnings or errors, but I think you're out of luck when you get a fatalError(). The javadoc says "SAX parsers are free to stop reporting any other events once this method has been invoked."

Is that what happens ... everything stops after fatalError()?
 
Tapas Mohapatra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
thanks for the reply!
No, the progem goes to error() only, and only once. It erros for the element required for the first element but there are other elements misssing in the xml, it does not display them.
Attaching the xml & xsd, if this helps.
xsd=>
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v2004 rel. 3 U (http://www.xmlspy.com) by Deanna Lataille (Systems Engineering Group, LLC) -->
<xs:schema elementFormDefault="qualified" attributeFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TaxExtract5498DetailVO">
<xs:annotation>
<xs ocumentation>Document to be fed into PRASE for the generation of GAAP Reserves </xs ocumentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="TaxExtractPK" type="xs:long"/>
<xs:element name="QualNonQualIndicator" type="xs:string"/>
<xs:element name="QualifiedType" type="xs:string"/>
<xs:element name="MarketingPackageName" type="xs:string" default="" minOccurs="0"/>
<xs:element name="BusinessContractName" type="xs:string" default="" minOccurs="0"/>
<xs:element name="OwnerDateOfBirth" type="xs:string"/>
<xs:element name="OwnerLastName" type="xs:string" default="" minOccurs="0"/>
<xs:element name="OwnerFirstName" type="xs:string" default="" minOccurs="0"/>
<xs:element name="OwnerCorporateName" type="xs:string" default="" minOccurs="0"/>
<xs:element name="AddressLine1" type="xs:string" default="" minOccurs="0"/>
<xs:element name="AddressLine2" type="xs:string" default="" minOccurs="0"/>
<xs:element name="AddressLine3" type="xs:string" default="" minOccurs="0"/>
<xs:element name="AddressLine4" type="xs:string" default="" minOccurs="0"/>
<xs:element name="City" type="xs:string" default="" minOccurs="0"/>
<xs:element name="State" type="xs:string" default="" minOccurs="0"/>
<xs:element name="Zip" type="xs:string" default="" minOccurs="0"/>
<xs:element name="TaxForm" type="xs:string"/>
<xs:element name="TaxYear" type="xs:string"/>
<xs:element name="TaxIdType" type="xs:string"/>
<xs:element name="TaxID" type="xs:string"/>
<xs:element name="ContractNumber" type="xs:string"/>
<xs:element name="AccumValue" type="xs:string"/>
<xs:element name="AccumulatedContribution" type="xs:string" default="" minOccurs="0"/>
<xs:element name="AccumulatedRollover" type="xs:string" default="" minOccurs="0"/>
<xs:element name="AccumulatedROTHConversion" type="xs:string" default="" minOccurs="0"/>
<xs:element name="SeventyAndHalfInd" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


xml=>
<?xml version="1.0" encoding="UTF-8"?>
<TaxExtract5498DetailVO><TaxExtractPK>1478</TaxExtractPK><MarketingPackageName></MarketingPackageName><BusinessContractName></BusinessContractName><OwnerLastName></OwnerLastName><OwnerFirstName></OwnerFirstName><OwnerCorporateName></OwnerCorporateName><AddressLine1></AddressLine1><AddressLine2></AddressLine2><AddressLine3></AddressLine3><AddressLine4></AddressLine4><Ci ty></City><State></State><Zip></Zip><TaxID>1111</TaxID><AccumulatedContribution>1234</AccumulatedContribution><AccumulatedRollover></AccumulatedRollover><AccumulatedROTHConversion></AccumulatedROTHConversion></TaxExtract5498DetailVO>
 
Tapas Mohapatra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
the problem got resolved.
My XSD had "<xs:sequence>" which was causing the problem.
We had to change it to "<xs:all>". Now all the errors are getting reported.

thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic