Posted in XML 12 years ago 1 min read
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Azhar
*/
public class CheckXML {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new SimpleErrorHandler());
Document doc = db.parse(new InputSource("worldbank.xml"));
}
private static class SimpleErrorHandler implements ErrorHandler {
public SimpleErrorHandler() {
}
@Override
public void warning(SAXParseException exception) throws SAXException {
System.out.println(exception.getMessage());
}
@Override
public void error(SAXParseException exception) throws SAXException {
System.out.println(exception.getMessage());
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println(exception.getMessage());
}
}
}
Semoga bermanfaat.
sumber: StackOverFlow