Hi Ragunath,
can you help me?
I have the following code and It doesnt validate properly if an error occurs:
Can you see what I am doing wrong??
Thanks alot,
Orla
package com.itservices.newsop.utils;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import com.itservices.newsop.utils.XmlErrorHandler;
import org.w3c.dom.Document;
import java.net.URL;
/**
*
* @author TDCGROR1
*/
public class XmlParser extends DefaultHandler{
protected static final String DEFAULT_PARSER_NAME =
"org.apache.xerces.parsers.SAXParser";
/** Creates a new instance of xmlutils */
public XmlParser() {
}
public void setDocumentLocator(Locator locator) {
super.setDocumentLocator(locator);
}
public void startDocument() throws SAXException {
super.startDocument();
}
public void endDocument() throws SAXException {
super.endDocument();
}
public void startElement(String s, String s1,String s2,Attributes
attributes) throws SAXException {
super.startElement(s,s1,s2,attributes);
}
public void endElement (String s, String s1, String s2) throws SAXException {
super.endElement(s,s1,s2);
}
public void characters(char[] chars, int i, int i1) throws SAXException {
super.characters(chars, i, i1);
}
public void ignorableWhitespace(char[] chars, int i, int i1)throws
SAXException {
super.ignorableWhitespace(chars, i, i1);
}
public void processingInstruction(String s, String s1) throws SAXException
{
super.processingInstruction(s, s1);
}
public void warning(SAXParseException ex) throws SAXException {
printError("Warning", ex);
}
public void error(SAXParseException ex) throws SAXException {
printError("Error", ex);
}
public void fatalError(SAXParseException ex) throws SAXException {
printError("Fatal Error", ex);
}
protected void printError(String type, SAXParseException ex) {
System.err.print("[");
System.err.print(type);
System.err.print("] ");
if (ex== null) {
System.out.println("!!!");
}
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1)
systemId = systemId.substring(index + 1);
System.err.print(systemId);
}
System.err.print(':');
System.err.print(ex.getLineNumber());
System.err.print(':');
System.err.print(ex.getColumnNumber());
System.err.print(": ");
System.err.print(ex.getMessage());
System.err.println();
System.err.flush();
}
public static void main( String[] argv ) {
try {
InputSource myInputSource = new
InputSource("C:\\xml\\customer.xml");
System.out.println("validating C:\\xml\\customer.xml");
xmlSchemaValidate(myInputSource,"file:///xml/customer.xsd");
xmlSchemaValidate(new
InputSource("file:///xml/networkSolution.xml"),"file:///xml/networkSolution.xsd");
} catch ( Exception ex ) {
ex.printStackTrace();
}
}
public static boolean xmlSchemaValidate(InputSource pInputSource, String
pSchema) {
//DOMParser parser = new DOMParser();
XMLReader parser = null;
try {
/*parser.setFeature( NAMESPACES_FEATURE_ID, true );
parser.setFeature( VALIDATION_FEATURE_ID, true ); */
parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
//XmlErrorHandler err = new XmlErrorHandler();
//parser.setErrorHandler(err);
System.out.println("xmlSchemaValidate - The Schema is " + pSchema);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
pSchema);
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
parser.setFeature("http://apache.org/xml/features/validation/dynamic", true);
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
true);
parser.parse(pInputSource);
}
catch (SAXException lxException){
System.out.println("SAXException");
//System.out.println("error: " + lxException.message());
lxException.printStackTrace();
System.out.println("Schema Validation Failed");
return(false);
}
catch (Exception ex) {
System.out.println("Exception");
ex.printStackTrace();
}
System.out.println("Schema Validation compare sucessful");
return(true);
}
}
-----Original Message-----
From: Ragunath Marudhachalam [mailto:[EMAIL PROTECTED]
Sent: Montag, 4. M�rz 2002 15:58
To: [EMAIL PROTECTED]
Subject: Help in validating.
Hi All,
I'm using xerces to validate my xml document with schema. I'm setting
ErrorHandler and i;m able to validate. When i get a exception, i'm able to
get the error message and the line number. Even though it gives me the
element name in the error message, is there any way i can get the element
name alone where the error has occured. I mean just like the line number, i
would like to get the element name.
Your help in this is highly appreciated!!!!
Thanks
Ragu
CircuitVision
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]