Java XML Transform Usage writeElementContent(XMLInputFactory inputFactory, XMLStreamWriter writer, Element element)

Here you can find the source of writeElementContent(XMLInputFactory inputFactory, XMLStreamWriter writer, Element element)

Description

write Element Content

License

Open Source License

Declaration

@Deprecated
    public static void writeElementContent(XMLInputFactory inputFactory, XMLStreamWriter writer, Element element)
            throws XMLStreamException 

Method Source Code

//package com.java2s;
/* //from   www.j  av a2  s .c o  m
 * 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.util.Iterator;

import javax.xml.stream.XMLEventReader;

import javax.xml.stream.XMLInputFactory;

import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;

import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Element;

public class Main {
    @Deprecated
    public static void writeElementContent(XMLInputFactory inputFactory, XMLStreamWriter writer, Element element)
            throws XMLStreamException {

        //http://stackoverflow.com/questions/7257508/convert-java-w3c-document-to-xmlstreamreader
        //http://bugs.sun.com/view_bug.do?bug_id=6631274
        DOMSource domSource = new DOMSource(element);
        XMLEventReader parser = inputFactory.createXMLEventReader(domSource);

        StartElement startElement;
        Characters characters;
        while (parser.hasNext()) {
            XMLEvent event = parser.nextEvent();
            switch (event.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                startElement = (StartElement) event;
                writer.writeStartElement(startElement.getName().getLocalPart());
                writeAttributes(writer, startElement.getAttributes());
                break;
            case XMLStreamConstants.END_ELEMENT:
                writer.writeEndElement();
                break;
            case XMLStreamConstants.CHARACTERS:
                characters = (Characters) event;
                writer.writeCharacters(characters.getData());
                break;
            default:
            }
        }
        parser.close();
    }

    public static void writeAttributes(XMLStreamWriter writer, Iterator attributes) throws XMLStreamException {
        while (attributes.hasNext()) {
            Attribute attr = (Attribute) attributes.next();
            writer.writeAttribute(attr.getName().getPrefix(), attr.getName().getNamespaceURI(),
                    attr.getName().getLocalPart(), attr.getValue());
        }
    }
}

Related

  1. streamSource(File file)
  2. toNonValidatingSAXSource(InputStream in)
  3. toXMLInputSource(StreamSource in)
  4. unwrapException(Throwable t)
  5. verifySignature(Element element, PublicKey validatingKey)
  6. writeXhtml(InputStream inputStream, Writer writer, Templates... templates)
  7. xsl(File inFile, File outFile, InputStream xslStream)