cancel
Showing results for 
Search instead for 
Did you mean: 

Java mapping for XSD Validation

shweta_walaskar2
Contributor
0 Kudos

Hello,

I have developed a Java mapping to validate an XML based on XSD.

I works fine and generates a message with error details.

I would like to create a report with all errors encountered during this Validation and to stop this message from being sent to the partner.

But this Java mapping stops at first error encountered and generates an error message with first error only even though document has more errors further.

Can anyone please let me know if there is an option to log all errors without allowing the message to go through?

Thanks.

Best Regards,

Shweta

Accepted Solutions (1)

Accepted Solutions (1)

rajasekhar_reddy14
Active Contributor
0 Kudos

if you are a good java programmer then you can catch al expcetions easily,refer this wiki

http://wiki.sdn.sap.com/wiki/display/XI/XIschemavalidationusingDOMparserviaJavacode

shweta_walaskar2
Contributor
0 Kudos

Thanks Raja,

but even the code in this link says:

If there is any schema validation error then the program will exit and

// StreamTransformationException will be thrown.

So it will behave in the same way.

Thanks.

Best Regards,

Shweta

rajasekhar_reddy14
Active Contributor
0 Kudos

insted of throwing an exception store expcetion message in string or exceptiuon object after parcing enitre document return all exception messages in one object and write it in to out object.

shweta_walaskar2
Contributor
0 Kudos

Hi Raja,

I have tried this. I am using validate method of Class Validator.

when I don't raise StreamTransformationException and pass error message in output , I still get first error in output.

Regards,

Shweta

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Yes, as abhishek mentioned when you validate the instance document (XML) using standard parsers then they validate based on the spec provided in XSD and they stop at the first error. The link given using dom parser via java code will also stop at the first error.

If you really want to validate the entire instance document then you might have to create a custom validator class which does validation for each and every fields and store it in an object like Vector or List or Map object. Then when you throw exception you can retrieve that Map object in the catch block and read all the individual error messages and display in log. This requires additional effort. In this case you dont rely on parser validation. Completely custom based.

Example:

Vector errorList = new Vector();

if the field name is SSN and if you see any alpha characters you add error for that field in the errorList

If(SSN.matches("[a-zA-Z]")){
    do nothing

}else{
    errorList.add("The fieldName SSN contains other than alpha characters");
}

Like that you have to add error string mesg in the errorList object and return the errorList at the end.

Answers (3)

Answers (3)

RaghuVamseedhar
Active Contributor
0 Kudos

Hi Shweta Walaskar,

If you want to log all validation errors (do not want to stop on first validation error), you need to implement ErrorHandler. Check this [Link|http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi/index.html#listing3].

Please note, it can catch only one fatal error.

Regards,

Raghu_Vamsee

former_member200962
Active Contributor
0 Kudos
But this Java mapping stops at first error encountered and generates an error message with first error only even though 
document has more errors further.

In my opinion it is the default property of the parser to stop at the first error and will not go any further.

shweta_walaskar2
Contributor
0 Kudos

Hi Abhishek,

Yes,I too think so. But it would be a valid requirement to get all error details at the same time,otherwise we need to keep on correcting each and every error by sending same document multiple times.

Any suggestions?

Thanks.

Regards,

Shweta

former_member854360
Active Contributor
0 Kudos

Hi,

You need to write your java validation code in that way so that total validation takes place.

append the errror details into a string

shweta_walaskar2
Contributor
0 Kudos

Hi Debashish,

Thanks. Do you have such code example?

Regards,

Shweta

former_member854360
Active Contributor
0 Kudos

Hi,

For Example please see

http://wiki.sdn.sap.com/wiki/display/XI/XIschemavalidationusingDOMparserviaJavacode