Friday, January 7, 2011

How to replace Doctype of xml using sax ?

Using SAX Entity Resolver you can change the DOCTYPE of xml .

Example :

String OnixFile=onixfilename; 
   
XMLReader readers = XMLReaderFactory.createXMLReader();
   
readers.setContentHandler(this);
readers.setErrorHandler(this);

readers.setEntityResolver(new Resolver()); 
FileReader filereader=new FileReader(OnixFile);
InputSource is=new InputSource(filereader);
is.setEncoding("UTF-8");
readers.parse(is);


class Resolver implements EntityResolver{

@Override
public InputSource resolveEntity(String publicId,String systemId)
   throws SAXException, IOException {
  if(systemId.equalsIgnoreCase(
    "http://www.editeur.org/onix/2.1/short
                     /onix-international.dtd")){

   String path="/ONIX 2.1 Revision 03 
                        reference/onix-international.dtd";
       return new InputSource(path);
 }
  return null;
 }
 
}

No comments:

Post a Comment