Wednesday, July 07, 2004

Using SAX Parser from validating XML document in Java 1.4

By default the SAX parser does not validate the XML document against DTD.
To validate a XML document, call setValidating(true) on SAXParserFactory instance.

Your handler class must override error(SAXParseException e) method of the DefaultHandler to throw an exception (see the example below). If you don't do this your xml document will be parsed and client(user/other program) will not know about the exception. The SAX parser calls this method if any error occurs while parsing. It is the responsibility of the handler writer to make this exception available to the external world.

The default implementation of error method is empty(i.e. it does not perform any operation).

In xml document DOCTYPE should be included. The parser takes the DTD from the DOCTYPE.

Example:

The below given java example prints the element in the console as they are encountered. If the example is executed with validbooks.xml no exception is thrown. But if the invalidbooks.xml is used exception will be thrown for <abc> tag.

If the error method is removed from the code, both the xml documents(invalidbooks.xml and validbooks.xml)is parsered without any exception.

DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT age (#PCDATA)>
<!ELEMENT book (person+)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)>
<!ELEMENT person (first,last,age)>





validbooks.xml

<?xml version="1.0"?>

<!DOCTYPE book SYSTEM "books.dtd">
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>




invalidbooks.xml

<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "books.dtd">
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<abc></abc>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>




Java Program:

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXTest extends DefaultHandler
{
public void startElement(String namespaceURI, String sName, String qName, Attributes attrs)
{
System.out.println("<" + qName + ">");
}

public void endElement(String namespaceURI, String sName, String qName)
{
System.out.println("");
}

public void error(SAXParseException e)throws SAXParseException
{
throw e;
}

public static void main(String[] args)
{
DefaultHandler handler = new SAXTest();

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
try
{
SAXParser parser = factory.newSAXParser();
System.out.println(parser.isValidating());
parser.parse("books.xml", handler);
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Monday, July 05, 2004

Headless in Java

Headless support is for application that want to process images but run on server which does not has display. Click
herefor more information.

Note: Java 1.4 onwards support headless and does not require X server.