• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing with SAX parser

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic