如何在JAVA中使用DOM Parser或SAX Parser从XML文档中删除/删除DOCTYPE声明?
如果你想知道的东西丢失了。请在评论中提及。
感谢
答案 0 :(得分:5)
这似乎可以做你想要的:
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;
}
}