Source Code : Resolving entities found in source XML during parsing
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Resolving entities found in source XML during parsing
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class Main {
public static void main(String[] argv) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
SAXParser parser = factory.newSAXParser();
SaxHandler handler = new SaxHandler();
parser.parse("sample.xml", handler);
}
}
class SaxHandler extends DefaultHandler {
public InputSource resolveEntity(String publicId, String systemId) {
if (systemId.equals("http://www.my-company.com/order-1.0.dtd")) {
return new InputSource(getClass().getResourceAsStream("order.dtd"));
} else {
return null;
}
}
public void startElement(String uri, String localName, String qName, Attributes attrs)
throws SAXException {
if (qName.equals("order")) {
}
}
public void error(SAXParseException ex) throws SAXException {
System.out.println("ERROR: [at " + ex.getLineNumber() + "] " + ex);
}
public void fatalError(SAXParseException ex) throws SAXException {
System.out.println("FATAL_ERROR: [at " + ex.getLineNumber() + "] " + ex);
}
public void warning(SAXParseException ex) throws SAXException {
System.out.println("WARNING: [at " + ex.getLineNumber() + "] " + ex);
}
}
Thank with us