RSS

How to Validate input XML with XSD using Java SAXParser

14 Dec

Hey Java champs,

Today i have started my blogging here. Hope you people enjoy glueing this blog..

Requirement : Parsing input XML with XSD using Java SAXParser

Solution:

Using SAXParserFactory and SchemaFactory, we can successfully achive the above requirement. Below is the working code posted for your reference. Hope this code saves your development time to some extent.

XMLParser.java

import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class XMLParser {
public static void main(String[] args) throws JAXBException {
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource(“contacts.xsd”)}));
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new SimpleErrorHandler());
// added below line to fix issue : Document root element “package”, must match DOCTYPE root “null”
reader.setFeature(“http://apache.org/xml/features/validation/dynamic”, true);
reader.parse(“contacts.xml”);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

SimpleErrorHandler.java

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

public class SimpleErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

public void error(SAXParseException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

public void fatalError(SAXParseException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

contacts.xsd

<?xml version=”1.0″ encoding=”UTF-8″?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”&gt;
<xs:element name=”contacts”>
<xs:complexType>
<xs:sequence>
<xs:element ref=”contact” />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=”contact”>
<xs:complexType>
<xs:sequence>
<xs:element name=”firstname” type=”xs:NCName” />
<xs:element name=”lastname” type=”xs:NCName” />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

contacts.xml

<contacts xsi:noNamespaceSchemaLocation=”contacts.xsd”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<contact >
<firstname>Edwin</firstname>
<lastname>Dankert</lastname>
</contact>
</contacts>

Issue

When i ran the application using the above code, i got below exception

org.xml.sax.SAXParseException: Document root element “package”, must match DOCTYPE root “null”

Solution

To resolve this issue, enable dynamic validation to parse the XML file by adding below line in the code.

XMLReader.setFeature(“http://apache.org/xml/features/validation/dynamic&#8221;, true);

Get glued to know more updates posts.

Have a nice day.

 

 

Advertisements
 
Leave a comment

Posted by on December 14, 2012 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: