Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Delete/Remove DOCTYPE Declaration from the XML Document?

Tags:

java

dom

xml

sax

jaxp

how to remove/delete the DOCTYPE declaration from the XML Document using DOM Parser or SAX Parser in JAVA?

If something you wanted to know is missing. Just mention it in your comments.

thanks

like image 610
Omkar Deekonda Avatar asked May 10 '11 12:05

Omkar Deekonda


1 Answers

This seems to do what you want :

try {
    XMLInputFactory inFactory = XMLInputFactory.newFactory();
    XMLOutputFactory outFactory = XMLOutputFactory.newFactory();

    XMLEventReader input = inFactory.createXMLEventReader(
            new FileInputStream("test.xml"));
    XMLEventReader filtered = inFactory.createFilteredReader(
            input, new DTDFilter());
    XMLEventWriter output = outFactory.createXMLEventWriter(
            System.out);

    output.add(filtered);
    output.flush();
}
catch (XMLStreamException e) {
    e.printStackTrace();
}
catch (FileNotFoundException e) {
    e.printStackTrace();
}

static class DTDFilter implements EventFilter
{
    @Override
    public boolean accept(XMLEvent event) {
        return event.getEventType() != XMLStreamConstants.DTD;
    }

}
like image 168
proactif Avatar answered Sep 28 '22 13:09

proactif