Avoid namespace while Parsing xml with woodstox

我是研究僧i 提交于 2019-12-11 17:06:42

问题


I am trying to parse an xml File and remove namespaces and prefix using woodstox parser(the xml contains nested elements and each element contains namespace at every level)

Below is the code i use to parse.I get the same input as i pass.Please help in resolving the issue

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:


If you want to remove all namespace prefixes and bindings, you should NOT use copy methods -- they will literally copy those things. Instead read element and attribute names, but only write out using "local name"s, and leave namespaceURI and prefix as nulls (or use methods that only take local name).



来源:https://stackoverflow.com/questions/53700912/avoid-namespace-while-parsing-xml-with-woodstox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!