File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes all validation error using saxParser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "all validation error using saxParser " Watch "all validation error using saxParser " New topic
Author

all validation error using saxParser

Tapas Mohapatra
Greenhorn

Joined: May 31, 2007
Posts: 3
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

Joined: Jan 29, 2003
Posts: 8791
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()?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tapas Mohapatra
Greenhorn

Joined: May 31, 2007
Posts: 3
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

Joined: May 31, 2007
Posts: 3
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: all validation error using saxParser
 
Similar Threads
SAX XSD Validation. Error callback is called twice.
Validating XML aginst XSD
Problem validating schema
DTD Validation problem via Sax
Validate xml against schema