// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XhtmlParse {
    public static void main(String[] args) throws Exception{
        String xml = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html>\r\n<head>\r\n<title>simple document</title>\r\n</head>\r\n<body>\r\n<p>a simple paragraph</p>\r\n</body>\r\n</html>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setEntityResolver(new DTDHandler());
        Document doc = db.parse(new InputSource(new StringReader(xml)));
    }
}

class DTDHandler implements EntityResolver {
    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        if(systemId.equals("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")) {
            return new InputSource("C:\\xhtml1-transitional.dtd");
        } else if(systemId.equals("http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent")) {
            return new InputSource("C:\\xhtml-lat1.ent");
        } else if(systemId.equals("http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent")) {
            return new InputSource("C:\\xhtml-symbol.ent");
        } else if(systemId.equals("http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent")) {
            return new InputSource("C:\\xhtml-special.ent");
        } else {
            return null;
        }
    }
}
