我正在尝试使用woodstox解析器解析xml文件并删除名称空间和前缀(xml包含嵌套元素,每个元素都包含每个级别的名称空间)
下面是我用来解析的代码。我得到的输入与我通过的相同。请帮助解决问题
byte[] byteArray = null;
try {
File file = new File(xmlFileName);
byteArray = new byte[(int) file.length()];
byteArray = FileUtils.readFileToByteArray(file);
} catch (Exception e) {
e.printStackTrace();
}
InputStream articleStream = new ByteArrayInputStream(byteArray);
WstxInputFactory xmlInputFactory = (WstxInputFactory) XMLInputFactory.newInstance();
xmlInputFactory.configureForSpeed();
// xmlInputFactory.configureForXmlConformance();
XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(articleStream,
StandardCharsets.UTF_8.name());
xmlStreamReader.setProperty(XMLInputFactory.IS_COALESCING, true);
WstxOutputFactory xmloutFactory = (WstxOutputFactory) XMLOutputFactory2.newInstance();
StringWriter sw = new StringWriter();
XMLEventWriter xw = null;
XMLStreamWriter2 xmlwriter = (XMLStreamWriter2) xmloutFactory.createXMLStreamWriter(sw,
StandardCharsets.UTF_8.name());
xmlwriter.setNamespaceContext(new NamespaceContext() {
@Override
public String getNamespaceURI(String prefix) {
return "";
}
@Override
public String getPrefix(String namespaceURI) {
return "";
}
@Override
public Iterator getPrefixes(String namespaceURI) {
return null;
}
});
while (xmlStreamReader.hasNext()) {
xmlStreamReader.next();
xmlwriter.copyEventFromReader(xmlStreamReader, false);
}
System.out.println("str" + xmlwriter.getNamespaceContext().getPrefix(""));
xmlwriter.closeCompletely();
xmlwriter.flush();
xmlStreamReader.closeCompletely();
xmlStreamReader.close();