getting exception while parsing using sax the xml file
843834Jan 3 2008 — edited Jan 3 2008hi,
I'm getting exception while parsing the xml file. This is my java file I'm using sax to parse the xml file...
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
public class CustAmlFinacle
{
/* method gets Cust_AML_Finacle.xsd , Cust_Aml_Finacle.xml and create an object for validator class.
Calling getSId() method of validator class which returns CustomerID,CustomerStatus,PassportStatus as pipe seperated String */
public String validateSchema(String SchemaUrl, String InputFile)
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setFeature("http://xml.org/sax/features/validation",true);
factory.setFeature("http://apache.org/xml/features/validation/schema", true);
factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
SAXParser parser = factory.newSAXParser();
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
File xmlFile = new File("temp.xml");
FileOutputStream fout = new FileOutputStream(xmlFile);
PrintStream out = new PrintStream(fout);
out.println(InputFile.toString());
// Create a ErrorHandler and set ErrorHandler on DocumentBuilderparser
Validator handler = new Validator();
// Parse XML Document
parser.parse(xmlFile, handler);
if (handler.validationError == true)
System.out.println("XML Document has Error:"
+ handler.validationError + " "
+ handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid");
return handler.getStatus(); //returns CustomerID,CustomerStatus,PassportStatus as pipe seperated String
}
catch (java.io.IOException ioe)
{
ioe.printStackTrace();
System.out.println("IOException " + ioe.getMessage());
}
catch (SAXException e)
{
System.out.println("SAXException" + e.getMessage());
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
System.out.println("ParserConfigurationException"+ e.getMessage());
e.printStackTrace();
}
return "";
}
// ErrorHandler Class
private class Validator extends DefaultHandler
{
public boolean validationError = false;
StringBuffer textBuffer;
String status = "";
public SAXParseException saxParseException = null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
public void endElement(String namespaceURI, String sName /* simple name */,
String qName /* qualified name */) throws SAXException
{
String eName = sName; // element name
if ("".equals(eName))
{
eName = qName; // not namespaceAware
}
String s = "" + textBuffer;
textBuffer = null;
status = status + s.trim() +"|" ;
}
public void characters(char[] buf, int offset, int len)throws SAXException
{
String s = new String(buf, offset, len);
if (textBuffer == null)
{
textBuffer = new StringBuffer(s);
}
else
{
textBuffer.append(s);
}
}
public String getStatus()
{
System.out.println(status);
return status;
}
}
public static void main(String[] argv)
{
String SchemaUrl = "Cust_AML_Finacle.xsd";
String InputFile = " <?xml version=\"1.0\" encoding=\"UTF-8\" ?><Cust_Finacle_AML xmlns=\"http://3i-infotech.com/Cust_AML_Finacle.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://3i-infotech.com/Cust_AML_Finacle.xsd\"><CustomerID>CustomerID</CustomerID><CustomerStatus>1</CustomerStatus><PassportStatus>1</PassportStatus></Cust_Finacle_AML> ";
CustAmlFinacle CustAmlFinacleObj = new CustAmlFinacle();
CustAmlFinacleObj.validateSchema(SchemaUrl, InputFile);
}
}
Exception Occured is :
SAXExceptionThe processing instruction target matching "[xX][mM][lL]" is not allowed....
Exception occured in this line : parser.parse(xmlFile, handler);