problem in using validating parser
Ruchi Goel <[email protected]>
| Newsgroups | gmane.text.xml.sax.general |
|---|---|
| Message-ID | <[email protected]> |
Hi ,
I am using te following code to validate my xml file against dtd.I am using crimson as SAX parser.But te code does not validate against my dtd. It does check for well formedness but does not check for validity.
import org.xml.sax.*; //HandlerBase--> deprecated
import org.xml.sax.helpers.*; //for Default handler
import java.io.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class saxTest extends DefaultHandler{
static private OutputStreamWriter out;
public static void main(String argv[]){
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
try{
// Set up output stream
out = new OutputStreamWriter (System.out, "UTF8");
//parse the input
SAXParser saxParser = factory.newSAXParser();
if (factory.isValidating())
System.out.println("parser is validating");
saxParser.parse(new File(argv[0]), new saxTest());
}catch (Throwable t) {
t.printStackTrace ();
}
}
//implementing DocumentHandler
public void startDocument() throws SAXException{
emit ("<?xml version='1.0' encoding='UTF-8'?>");
nl();
}
public void endDocument() throws SAXException{
try {
nl();
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException{
nl();
emit ("<"+name);
//System.out.print("after element print" + name);
if (attrs != null) {
for (int i = 0; i < attrs.getLength (); i++) {
emit (" ");
emit (attrs.getQName(i)+"=\""+attrs.getValue (i)+"\"");
}
}
emit (">");
//nl();
}
public void endElement(String uri , String localName,String name) throws SAXException{
emit ("</"+name+">");
nl();
}
public void characters(char[] ch, int start, int length) throws SAXException{
String s = new String(ch, start, length);
emit (s);
}
private void emit (String s)throws SAXException{
try {
out.write (s);
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
private void nl ()
throws SAXException
{
//String lineEnd = System.getProperty("line.separator");
String lineEnd = "\n" ;
try {
out.write (lineEnd);
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
}
Can somebody tell what is the mistake in the code.
Thanks in advance,
Ruchi
---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now