SAXParserFactory: setValidating(boolean validating)
/*
Parser will be namespace aware
Parser will validate XML
Parser object is: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl@21b6d
*/
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class MainClass {
public static void main(String args[]) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ")
+ "be namespace aware");
System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");
SAXParser parser = null;
try {
parser = spf.newSAXParser();
} catch (ParserConfigurationException e) {
e.printStackTrace(System.err);
} catch (SAXException e) {
e.printStackTrace(System.err);
}
System.out.println("Parser object is: " + parser);
}
}
Related examples in the same category