Example usage for javax.xml.stream XMLOutputFactory newInstance

List of usage examples for javax.xml.stream XMLOutputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory newInstance.

Prototype

public static XMLOutputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:Main.java

public static String convertBean2Xml(Object obj) throws IOException {
    String result = null;//from   ww w .ja v  a2 s.  c o m
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
                (String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
        xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
        marshaller.marshal(obj, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
        result = baos.toString("UTF-8");

    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    } catch (XMLStreamException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:Main.java

/**
 * This will returns the instance of the STAX XMLOutputFactory and will initialze the properties to the factory.
 *
 * @return This will return the instance of STAX XMLOutputFactory.
 *///from   ww w  . ja  v a2  s.co  m
public static XMLOutputFactory getStaxOutputFactory() {

    if (staxOutputFactory == null) {
        try {
            // These should be taken from either properties file or from command line arguments.
            System.setProperty("javax.xml.stream.XMLOutputFactory", "com.ctc.wstx.stax.WstxOutputFactory");
            staxOutputFactory = XMLOutputFactory.newInstance();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return staxOutputFactory;
}

From source file:Main.java

/**
 * Get the XMLOutputFactory./*from   w  w w  . j  a va2 s  .  c o m*/
 * 
 * @return the XMLOutputFactory
 */
public static XMLOutputFactory getStaxWriterFactory() {
    if (s_staxWriterFactory == null) {
        s_staxWriterFactory = XMLOutputFactory.newInstance();
    }
    return s_staxWriterFactory;
}

From source file:Main.java

private static XMLStreamWriter newWriter(ByteArrayOutputStream result) throws Exception {
    XMLOutputFactory output = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = output.createXMLStreamWriter(result);
    return writer;
}

From source file:Main.java

/**
 * //from  ww w  . j  av a  2s. c om
 * @param is
 * @param os
 * @param elementNames
 * @throws XMLStreamException
 * @throws FactoryConfigurationError
 * @throws UnsupportedEncodingException
 */
public static void stripElements(final InputStream is, final OutputStream os,
        final Collection<String> elementNames)
        throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
    final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(os);

    String elementName = null;

    while (xmlEventReader.peek() != null) {
        final XMLEvent event = (XMLEvent) xmlEventReader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            final StartElement startElement = event.asStartElement();
            final QName name = startElement.getName();

            if (elementNames.contains(name.getLocalPart())) {
                elementName = name.getLocalPart();
            }

            if (elementName == null) {
                xmlEventWriter.add(event);
            }

            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            final EndElement endElement = event.asEndElement();
            final QName name = endElement.getName();

            if (elementName == null) {
                xmlEventWriter.add(event);
            } else if (elementName.equals(name.getLocalPart())) {
                elementName = null;
            }

            break;
        }
        default: {
            if (elementName == null) {
                xmlEventWriter.add(event);
            }
        }
        }
    }

    xmlEventWriter.flush();
}

From source file:Main.java

/**
 * //from w w  w.ja  va2 s  .c  om
 * @param elementName
 * @param is
 * @param onlyValues
 * @return Collection
 * @throws XMLStreamException
 * @throws UnsupportedEncodingException
 */
public static Collection<String> getElements(final String elementName, final InputStream is,
        final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException {
    final Collection<String> elements = new ArrayList<>();
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final XMLEventReader reader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter writer = XMLOutputFactory.newInstance()
            .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name()));
    boolean read = false;
    String characters = null;

    while (reader.peek() != null) {
        final XMLEvent event = (XMLEvent) reader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            read = read || elementName.equals(event.asStartElement().getName().getLocalPart());

            if (read && !onlyValues) {
                writer.add(event);
            }

            break;
        }
        case XMLStreamConstants.ATTRIBUTE: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            characters = event.asCharacters().getData();
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            if (elementName.equals(event.asEndElement().getName().getLocalPart())) {
                writer.flush();

                if (characters != null) {
                    elements.add(characters);
                }

                os.reset();
                read = false;
            }
            break;
        }
        default: {
            // Ignore
            break;
        }
        }
    }

    return elements;
}

From source file:diuf.unifr.ch.first.xwot.rxtx.notifications.JAXBEntity.java

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    XMLOutputFactory xmloutputf = XMLOutputFactory.newInstance();
    try {/*  w w w.  j a  va2s.  c o m*/
        XMLStreamWriter writer = xmloutputf.createXMLStreamWriter(outstream, "UTF-8");
        // do the actual writing using an appropriate Marshaller 
        // and do not forget to flush
        writer.flush();
    } catch (XMLStreamException ex) {
        // Re-throw appropriate i/o or runtime exception
        throw new IOException("Oppsie");
    }
}

From source file:com.norconex.committer.gsa.XmlOutput.java

public XmlOutput(OutputStream out) throws XMLStreamException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    writer = factory.createXMLStreamWriter(out, CharEncoding.UTF_8);
}

From source file:com.esri.geoportal.commons.meta.js.XmlBuilder.java

/**
 * Initialize the builder.//w w w . j a v a  2  s.  c o  m
 * @throws Exception if initialization fails
 */
public void init() throws Exception {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    xml = new StringWriter();
    writer = factory.createXMLStreamWriter(xml);
}

From source file:com.norconex.commons.lang.xml.EnhancedXMLStreamWriter.java

public EnhancedXMLStreamWriter(Writer out, boolean writeBlanks) throws XMLStreamException {
    super();/*from   ww w  .j  a  v  a2 s.  c o m*/
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    writer = factory.createXMLStreamWriter(out);
    this.writeBlanks = writeBlanks;
}