0

我正在尝试使用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();
4

1 回答 1

0

如果你想删除所有命名空间前缀和绑定,你不应该使用复制方法——它们会复制这些东西。而是读取元素和属性名称,但仅使用“本地名称”写出,并将namespaceURIprefix作为空值(或使用仅采用本地名称的方法)。

于 2018-12-14T05:00:42.490 回答