Java XML Parse Stream getParamStream(XMLOutputFactory outputFactory, XMLEventFactory eventFactory, XMLEventReader parser, XMLEvent paramEvent)

Here you can find the source of getParamStream(XMLOutputFactory outputFactory, XMLEventFactory eventFactory, XMLEventReader parser, XMLEvent paramEvent)

Description

get Param Stream

License

Open Source License

Declaration

public static byte[] getParamStream(XMLOutputFactory outputFactory, XMLEventFactory eventFactory,
            XMLEventReader parser, XMLEvent paramEvent) throws XMLStreamException, IOException 

Method Source Code


//package com.java2s;
/* /*from www . j  av a 2 s.  com*/
 * Copyright (c) 2012-present Jan To?ovsk? <jan.tosovsky.cz@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;

import javax.xml.stream.XMLOutputFactory;

import javax.xml.stream.XMLStreamException;

import javax.xml.stream.events.XMLEvent;

public class Main {
    public static byte[] getParamStream(XMLOutputFactory outputFactory, XMLEventFactory eventFactory,
            XMLEventReader parser, XMLEvent paramEvent) throws XMLStreamException, IOException {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLEventWriter writer = outputFactory.createXMLEventWriter(outputStream);

        // this skips all the attributes of the original <g> element to avoid ID duplications
        writer.add(eventFactory.createStartElement("", null, "g"));

        Integer level = 1;
        while (!(parser.hasNext() && paramEvent.isEndElement() && level == 0)) {
            paramEvent = parser.nextEvent();
            writer.add(paramEvent);
            if (paramEvent.isStartElement()) {
                level++;
            }
            if (paramEvent.isEndElement()) {
                level--;
            }
        }

        writer.flush();
        writer.close();

        return outputStream.toByteArray();
    }
}

Related

  1. getNextStartElement(XMLStreamReader parser)
  2. parse(DefaultHandler handler, InputStream in)
  3. parse(final InputStream in)
  4. parse(final InputStream inputStream)
  5. parse(final InputStream is)