Hi,
I wrote java code for xml validation against xsd file.In that if any error in xml it throws the exception.Instead of that i want that program to return result in string like what is the problem.
package com.xmlpack;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
public class SampleXmlValidator {
public static void main(String args[]) throws SAXException, IOException {
Source xmlFile = new StreamSource(new File(
"D:/SampleXML/sample/src/com/xml/product.xml"));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(
"D:/SampleXML/sample/src/com/xsd/product.xsd"));
Validator validator = schema.newValidator();
try {
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
}
}
in google search i found that we can use validator.validate(Source,Result) method.
I want to know how to implement the method..