Examples of Input Validation Failures


JAXP with Xerces and MSXML report validation errors just a bit differently, but they take essentially the same approach. Let's look at a few examples from validating XML instance documents as input. Unless otherwise noted, all instance documents use the CSVFile.xsd schema. I have omitted the Java stack trace.

Here's what we see when there are no Row Elements.

JAXP with Xerces
[View full width]
 org.xml.sax.SAXException: Error during parsing Text = cvc-complex-type.2.4.b: The content of element 'CSVFile' is not complete. It must graphics/ccc.gif match '("":Row){1-UNBOUNDED} '. URL = file:e:/book/chapters/ch05/csvfilenorows.xml Entity = null Line = 3 Column = 11 
MSXML
 Parsing Error Reason : Element cannot be empty according to the DTD/Schema. Source Text : </CSVFile> File URL : file:///e:/book/chapters/ch05/csvfilenorows.xml Position in file = 175 Line           = 3 Position in line = 3 

Here's what we see when we have an unexpected element, Column999, as a child of the Row Element.

JAXP with Xerces
 org.xml.sax.SAXException: Error during parsing Text = cvc-complex-type.2.4.a: Invalid content starting with element 'Column999'. The content must match ((((((((((((((((((((((((((((((((( (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( ("":Column01){0-1},("":Column02){0-1}),("":Column03){0-1}),("":Column04){0-1}), ("":Column05){0-1}),("":Column06){0-1}),("":Column07){0-1}),("":Column08){0-1}), ("":Column09){0-1}),("":Column10){0-1}),("":Column11){0-1}),("":Column12){0-1}), ("":Column13){0-1}),("":Column14){0-1}),("":Column15){0-1}),("":Column16){0-1}), ("":Column17){0-1}),("":Column18){0-1}),("":Column19){0-1}),("":Column20){0-1}), ("":Column21){0-1}),("":Column22){0-1}),("":Column23){0-1}),("":Column24){0-1}), ("":Column25){0-1}),("":Column26){0-1}),("":Column27){0-1}),("":Column28){0-1}), ("":Column29){0-1}),("":Column30){0-1}),("":Column31){0-1}),("":Column32){0-1}), ("":Column33){0-1}),("":Column34){0-1}),("":Column35){0-1}),("":Column36){0-1}), ("":Column37){0-1}),("":Column38){0-1}),("":Column39){0-1}),("":Column40){0-1}), ("":Column41){0-1}),("":Column42){0-1}),("":Column43){0-1}),("":Column44){0-1}), ("":Column45){0-1}),("":Column46){0-1}),("":Column47){0-1}),("":Column48){0-1}), ("":Column49){0-1}),("":Column50){0-1}),("":Column51){0-1}),("":Column52){0-1}), ("":Column53){0-1}),("":Column54){0-1}),("":Column55){0-1}),("":Column56){0-1}), ("":Column57){0-1}),("":Column58){0-1}),("":Column59){0-1}),("":Column60){0-1}), ("":Column61){0-1}),("":Column62){0-1}),("":Column63){0-1}),("":Column64){0-1}), ("":Column65){0-1}),("":Column66){0-1}),("":Column67){0-1}),("":Column68){0-1}), ("":Column69){0-1}),("":Column70){0-1}),("":Column71){0-1}),("":Column72){0-1}), ("":Column73){0-1}),("":Column74){0-1}),("":Column75){0-1}),("":Column76){0-1}), ("":Column77){0-1}),("":Column78){0-1}),("":Column79){0-1}),("":Column80){0-1}), ("":Column81){0-1}),("":Column82){0-1}),("":Column83){0-1}),("":Column84){0-1}), ("":Column85){0-1}),("":Column86){0-1}),("":Column87){0-1}),("":Column88){0-1}), ("":Column89){0-1}),("":Column90){0-1}),("":Column91){0-1}),("":Column92){0-1}), ("":Column93){0-1}),("":Column94){0-1}),("":Column95){0-1}),("":Column96){0-1}), ("":Column97){0-1}),("":Column98){0-1}),("":Column99){0-1})'. URL = file:e:/book/chapters/ch05/csvfilecolumn999.xml Entity = null Line = 12 Column = 16 
MSXML
 Parsing Error Reason : Element content is invalid according to the DTD/Schema. Expecting: Column09, Column10, Column11, Column12, Column13, Column14, Column15, Column16, Column17, Column18, Column19, Column20, Column21, Column22, Column23, Column24, Column25, Column26, Column27, Column28, Column29, Column30, Column31, Column32, Column33, Column34, Column35, Column36, Column37, Column38, Column39, Column40, Column41, Column42, Column43, Column44, Column45, Column46, Column47, Column48, Column49, Column50, Column51, Column52, Column53, Column54, Column55, Column56, Column57, Column58, Column59, Column60, Column61, Column62, Column63, Column64, Column65, Column66, Column67, Column68, Column69, Column70, Column71, Column72, Column73, Column74, Column75, Column76, Column77, Column78, Column79, Column80, Column81, Column82, Column83, Column84, Column85, Column86, Column87, Column88, Column89, Column90, Column91, Column92, Column93, Column94, Column95, Column96, Column97, Column98, Column99. Source Text :   <Column999>972-996-1051</Column999> File URL : file:///e:/book/chapters/ch05/csvfilecolumn999.xml Position in file = 468 Line           = 12 Position in line = 16 

Here's what we see when Column11 is a date type, and the date is not in the correct format.

JAXP with Xerces
 org.xml.sax.SAXException: Error during parsing Text = cvc-type.3.1.3: The value '15-May-2002' of element 'Column11' is not valid. URL = file:e:/book/chapters/ch05/csvfiledate.xml Entity = null Line = 13 Column = 37 
MSXML
 Parsing Error Reason : The element: 'Column11'  has an invalid value according to its data type. Source Text :   <Column11>15-May-2002</Column11> File URL : file:///e:/book/chapters/ch05/csvfiledate.xml Position in file = 523 Line           = 13 Position in line = 28 

Here's what we see when we specify validation on the command line, but the instance document doesn't have a reference to a schema.

JAXP with Xerces
 org.xml.sax.SAXException: Error during parsing Text = cvc-elt.1: Cannot find the declaration of element 'SimpleCSV'. URL = file:E:/Book/JavaCode/CH05/XMLToCSV/SimpleCSV.XML Entity = null Line = 2 Column = 12 
MSXML
 Validation Error Reason : Validate failed because the root element had no associated DTD/schema. Position in file = 0 Line           = 0 Position in line = 0 

Finally, here's what we see when one of our xsd:element declarations in the schema is invalid.

JAXP with Xerces
[View full width]
 org.xml.sax.SAXException: Error during parsing Text = Error:  sequence content must be zero or more of element, group, choice, sequence graphics/ccc.gif or any.  Saw "{1} ". URL = file:///E:/Book/Schemas/CSVFileBad.xsd Entity = null Line = 34 Column = 72 
MSXML
 Validation Error Reason : Validate failed because the root element had no associated DTD/schema. Position in file = 0 Line           = 0 Position in line = 0 

Note that in this final example JAXP reported the URL of the schema file and not the instance document. With MSXML we got a validation error reported instead of a parsing error. My guess is that MSXML hit the parsing error on the schema file a bit farther down the stack. However, since we rely on MSXML to do a lot of the work in the background for us and we didn't explicitly set up a schema cache and load the schema file, MSXML probably lost the specific information on the schema file as MSXML unwound the stack.

Remember our discussion about MSXML validating during parsing? In the first three examples above we saw that the failure started with our program text "Parsing Error", while the last two started with "Validation Error". This indicates that, even though all the errors in these examples were technically validation errors rather than parsing errors, they were actually encountered in the call to the load method and handled as parsing errors rather than being encountered in the call to the validate method. Note that we also saw the position in the file, the line number, and the position in the line. If we had loaded without validation and then just called the validate method, we would have gotten a validation error and not received this level of detail. Note also that the errors in the last two examples were reported as validation errors rather than as parsing errors. MSXML didn't find these schema problems while loading!

These examples again reveal an interesting contrast between JAXP with Xerces and MSXML. We have coded both implementations of our utility to do the same things. They validate only if we tell them to. They both report errors if we requested validation and the instance document doesn't reference a valid schema. However, because of the differences in the APIs' behaviors we have to take somewhat different approaches. JAXP won't validate unless we tell it to, and if it does validate it will throw an exception if the instance document doesn't declare a schema. MSXML validates on parsing by default, but it doesn't report a parsing error if no schema reference was found. We have to make an additional call to the validate method to determine that. There's still a lot of inconsistency in the XML world. I hope that DOM Level 3 clears up some of this, but I'm not holding my breath .

Validation is indeed a useful tool. We'll see that judicious use of schema validation can save us from writing some validation code in the utilities we'll be developing in later chapters. Through the discussions in the previous chapter and continued discussions throughout the rest of the book, you should begin to get some ideas about how you can use validation to your benefit. You may also begin to see where it might get in your way!



Using XML with Legacy Business Applications
Using XML with Legacy Business Applications
ISBN: 0321154940
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net