简体   繁体   中英

Avoid namespace while Parsing xml with woodstox

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();

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM