如何在 JAVA 中使用 DOM Parser 或 SAX Parser 从 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;
}
}