如何使用DOM解析器或SAX解析器从XML文档中删除DOCTYPE声明?如果您需要了解的内容有所缺失,请在评论中提及。谢谢!
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;
}
}
code
<!DOCTYPE ~~~filepath~~~ >code
。 - Omkar Deekonda