Avoid namespace while Parsing xml with woodstox

740 Views Asked by At

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

There are 1 best solutions below

0
StaxMan On

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