| Author |
Parsing with SAX parser
|
Ismail Jaffer
Greenhorn
Joined: Mar 20, 2006
Posts: 2
|
|
I need to validate an XML document against its shcema. I have the following code. This code validates XML schema (check.xsd) and XML document (check.xml) individually but XML is not validated againtst XML schema (like sequence ,enumeration etc.) I am using xerces parser come with Java 1.5 Do I need to set some property for doing this. Please some one help me. Thanks for help. ========================================================================== import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import javax.xml.parsers.ParserConfigurationException; import javax.xml.validation.SchemaFactory; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.*; import java.io.File; import java.io.IOException; import java.util.*; import static javax.xml.XMLConstants.*; public class ParsingSchemaInstance { public static void main(String args[]) { File xmlFile = new File("d:\\hhh\\check.xml"); File schemaFile = new File("d:\\hhh\\check.xsd"); process(xmlFile, schemaFile); } private static void process(File file, File schemaFile) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null; spf.setNamespaceAware(true); try { SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); spf.setSchema(sf.newSchema(schemaFile)); parser = spf.newSAXParser(); } catch(SAXException e) { e.printStackTrace(System.err); System.exit(1); } catch(ParserConfigurationException e) { e.printStackTrace(System.err); System.exit(1); } =========================================================================== Handler code ----------- import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXParseException; import org.xml.sax.SAXException; import java.util.*; public class SAXHandler extends DefaultHandler { HashMap<String,Long> amap = new HashMap<String,Long> (); public void startDocument() { System.out.println("Start document: "); } public void endDocument() { System.out.println("End document: "); Iterator<String> keyIter = amap.keySet().iterator(); String temp1; while(keyIter.hasNext()) { temp1 = keyIter.next(); System.out.println("Element: " + temp1 + " -- " + "Count : " + amap.get(temp1)); } } public void startElement(String uri, String localName, String qname, Attributes attr) { Long oldvalue = null; oldvalue = amap.get(qname); if (oldvalue == null) amap.put(qname,1L); else amap.put(qname,oldvalue + 1); } public void warning(SAXParseException spe) { System.out.println("Warning at line "+spe.getLineNumber()); System.out.println(spe.getMessage()); } public void fatalError(SAXParseException spe) throws SAXException { System.out.println("Fatal error at line "+spe.getLineNumber()); System.out.println(spe.getMessage()); throw spe; } } ===========================================================================
|
 |
Ismail Jaffer
Greenhorn
Joined: Mar 20, 2006
Posts: 2
|
|
Hello, Please some one help me. I am looking for parser setting required to validate XML docuemnt with its corresponding XSD. I am using parser provided with J2SE 1.5 Thanks, With Regards, Ismail J
|
 |
 |
|
|
subject: Parsing with SAX parser
|
|
|