Saturday, 9 April 2011

Checking for Well-Formed XML

Checking For Well-Formed XML

In several SOA projects I’ve worked on the test team generate their test XML by hand, which is a long winded, error prone business. The problem with this is that stuffing lots of broken XML into your code generates lots of invalid bugs. A top tip to negate this problem is to always check that any XML passed as arguments to your code is well-formed - and do this first. The code below demonstrates how to do this using a validator class.

public class WellFormedValidator {

 
/**
   * This is parsing code
   *
   *
@param xml The input argument to check.
   *
@throws SAXException
   *             If the input xml is invalid
   *
   *
@throws SystemException
   *             Thrown if the input string cannot be read
   */
 
public void validate(String xml) throws SAXException {

   
try {
     
doValidate(xml);
   
} catch (IOException ioe) {
     
throw new SystemException(
         
"Cannot parse input string. Message:" + ioe.getMessage(), ioe);
   
}
  }

 
private void doValidate(String xml) throws SAXException, IOException {
   
XMLReader parser = XMLReaderFactory.createXMLReader();
    parser.setContentHandler
(new DefaultHandler());
    InputSource source =
new InputSource(new ByteArrayInputStream(xml.getBytes()));
    parser.parse
(source);
 
}

}

There are some points to note about this: on the design front, I’m using the checked SAXException to indicate validation failure. in this case it’s better than using a straight forward boolean true or false because the exception contains additional information that’ll help you to fix any duff XML. Should anything else go wrong, then the validator throws a runtime exception. This is in keeping with the exception strategy highlighed in my previous blog. And, in adding an additional layer into your mix, you’ll take a slight performance hit, but I reckon that it’s worth it as it saves a lot of time.

No comments: