使用woodstox解析xml时避免命名空间

时间:2018-12-10 07:02:10

标签: java xml stax woodstox

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

1 个答案:

答案 0 :(得分:0)

如果要删除所有名称空间前缀和绑定,则不应使用复制方法-它们将按字面意义复制这些内容。而是读取元素和属性名称,但仅使用“本地名称”写出,并将namespaceURIprefix保留为空(或使用仅采用本地名称的方法)。