Example usage for javax.xml.stream XMLEventWriter flush

List of usage examples for javax.xml.stream XMLEventWriter flush

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventWriter flush.

Prototype

public void flush() throws XMLStreamException;

Source Link

Document

Writes any cached events to the underlying output mechanism

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setXMLResolver(new XMLResolver() {
        public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
                throws XMLStreamException {
            System.out.println(systemID);
            return null;
        }/*  w w w  .j a  v a2s  . c  om*/
    });

    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream("dtd.xml"));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
    writer.add(reader);
    writer.flush();

}

From source file:StaxEvent.java

public static void main(String[] args) throws Exception {
    StaxEvent ms = new StaxEvent();

    XMLEventReader reader = XMLInputFactory.newInstance()
            .createXMLEventReader(new java.io.FileInputStream("yourXML.xml"));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    while (reader.hasNext()) {
        XMLEvent event = (XMLEvent) reader.next();

        if (event.getEventType() == event.CHARACTERS) {
            writer.add(ms.getNewCharactersEvent(event.asCharacters()));
        } else {/*from ww  w.  j  av a2  s.  com*/
            writer.add(event);
        }
    }
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    writer.add(eventFactory.createStartElement("ns1", "http://www.e.com/ns1", "sample", null, null));
    writer.add(eventFactory.createNamespace("ns1", "http://www.e.com/ns1"));
    writer.add(eventFactory.createNamespace("ns2", "http://www.e.com/ns2"));
    writer.add(eventFactory.createAttribute("ns2", "http://www.e.com/ns2", "attribute", "true"));
    writer.add(eventFactory.createEndDocument());
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out);

    writer.add(eventFactory.createStartDocument("UTF-8", "1.0"));
    writer.add(eventFactory.createStartElement(new QName("p"), null, null));
    XMLEvent sampleElement = eventFactory.createStartElement("", null, "s", null, null);
    writer.add(sampleElement);//from w w w  .ja  v a2 s .  co  m
    writer.add(eventFactory.createEndElement("", null, "s"));
    writer.add(sampleElement);
    writer.add(eventFactory.createEndDocument());
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    Namespace ns1 = eventFactory.createNamespace("ns1", "http://www.e.com/ns1");
    Namespace ns2 = eventFactory.createNamespace("ns2", "http://www.e.com/ns2");
    List<Namespace> namespaceList = new ArrayList<Namespace>();
    namespaceList.add(ns1);//from w w w  .  j  a  v  a  2s .  c  om
    namespaceList.add(ns2);

    Attribute attribute = eventFactory.createAttribute(ns2.getPrefix(), ns2.getNamespaceURI(), "attribute",
            "true");

    writer.add(eventFactory.createStartElement(ns1.getPrefix(), ns1.getNamespaceURI(), "sample",
            Collections.singletonList(attribute).iterator(), namespaceList.iterator()));
    writer.add(eventFactory.createEndDocument());
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out);
    XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();

    StartDocument startDocument = xmlEventFactory.createStartDocument("UTF-8", "1.0");
    writer.add(startDocument);/*from ww  w  .ja  v a2s .  c  om*/

    StartElement startElement = xmlEventFactory.createStartElement("", "", "My-list");
    writer.add(startElement);

    Attribute attribute = xmlEventFactory.createAttribute("version", "1");
    List attributeList = Arrays.asList(attribute);
    List nsList = Arrays.asList();
    StartElement startElement2 = xmlEventFactory.createStartElement("", "", "Item", attributeList.iterator(),
            nsList.iterator());
    writer.add(startElement2);

    StartElement codeSE = xmlEventFactory.createStartElement("", "", "code");
    writer.add(codeSE);
    Characters codeChars = xmlEventFactory.createCharacters("I001");
    writer.add(codeChars);
    EndElement codeEE = xmlEventFactory.createEndElement("", "", "code");
    writer.add(codeEE);

    StartElement nameSE = xmlEventFactory.createStartElement(" ", " ", "name");
    writer.add(nameSE);
    Characters nameChars = xmlEventFactory.createCharacters("a name");
    writer.add(nameChars);
    EndElement nameEE = xmlEventFactory.createEndElement("", "", "name");
    writer.add(nameEE);

    StartElement contactSE = xmlEventFactory.createStartElement("", "", "contact");
    writer.add(contactSE);
    Characters contactChars = xmlEventFactory.createCharacters("another name");
    writer.add(contactChars);
    EndElement contactEE = xmlEventFactory.createEndElement("", "", "contact");
    writer.add(contactEE);

    EndDocument ed = xmlEventFactory.createEndDocument();
    writer.add(ed);

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

From source file:XMLEventWriterDemo.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

    XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out);

    XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();

    StartDocument startDocument = xmlEventFactory.createStartDocument("UTF-8", "1.0");
    writer.add(startDocument);/*from   ww  w. j a  v  a  2s  .  c  o  m*/

    StartElement startElement = xmlEventFactory.createStartElement("", "", "My-list");
    writer.add(startElement);

    Attribute attribute = xmlEventFactory.createAttribute("version", "1");
    List attributeList = Arrays.asList(attribute);
    List nsList = Arrays.asList();
    StartElement startElement2 = xmlEventFactory.createStartElement("", "", "Item", attributeList.iterator(),
            nsList.iterator());
    writer.add(startElement2);

    StartElement codeSE = xmlEventFactory.createStartElement("", "", "code");
    writer.add(codeSE);
    Characters codeChars = xmlEventFactory.createCharacters("I001");
    writer.add(codeChars);
    EndElement codeEE = xmlEventFactory.createEndElement("", "", "code");
    writer.add(codeEE);

    StartElement nameSE = xmlEventFactory.createStartElement(" ", " ", "name");
    writer.add(nameSE);
    Characters nameChars = xmlEventFactory.createCharacters("a name");
    writer.add(nameChars);
    EndElement nameEE = xmlEventFactory.createEndElement("", "", "name");
    writer.add(nameEE);

    StartElement contactSE = xmlEventFactory.createStartElement("", "", "contact");
    writer.add(contactSE);
    Characters contactChars = xmlEventFactory.createCharacters("another name");
    writer.add(contactChars);
    EndElement contactEE = xmlEventFactory.createEndElement("", "", "contact");
    writer.add(contactEE);

    EndDocument ed = xmlEventFactory.createEndDocument();
    writer.add(ed);

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

From source file:Main.java

/**
 * Copy an XML event stream.//from  w w  w . j  a  v  a  2s  .c om
 * @param reader The event reader.
 * @param writer The event writer.
 * @param omitDoc if true, ignore start/end document events.
 * @throws XMLStreamException For errors writing to the XML event writer.
 */
public static void copyXMLEventStream(final XMLEventReader reader, final XMLEventWriter writer,
        final boolean omitDoc) throws XMLStreamException {
    if (omitDoc) {
        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();
            final int type = event.getEventType();
            if ((type != XMLStreamConstants.START_DOCUMENT) && (type != XMLStreamConstants.END_DOCUMENT)) {
                writer.add(event);
            }
        }
    } else {
        writer.add(reader);
    }
    writer.flush();
}

From source file:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java

private static String slurpXMLData(XMLEventReader parser, StartElement sevent)
        throws XML2HTTPException, XMLStreamException {
    StringWriter bodyStringWriter = new StringWriter();
    XMLEventWriter bodyWriter = null;
    int depth = 0;
    synchronized (xmlOutputFactory) {
        bodyWriter = xmlOutputFactory.createXMLEventWriter(bodyStringWriter);
    }//from   w  w w .  j a  va  2 s.  co m
    while (parser.hasNext()) {
        XMLEvent event = parser.nextEvent();

        if (event.isEndElement() && depth == 0) {
            bodyWriter.flush();
            return bodyStringWriter.toString();
        }
        bodyWriter.add(event);
        if (event.isStartElement())
            depth++;
        else if (event.isEndElement())
            depth--;
    }
    throw new XML2HTTPException("Early end of file while reading inner XML document.");
}

From source file:org.javelin.sws.ext.bind.SweJaxbMarshaller.java

@Override
public void marshal(Object jaxbElement, Result result) throws JAXBException {
    try {// ww  w . jav  a 2s .  c  o m
        XMLEventWriter writer = this.xmlOutputFactory.createXMLEventWriter(result);
        this.marshal(jaxbElement, writer);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new JAXBException(e.getMessage(), e);
    }
}