Example usage for javax.xml.stream XMLEventWriter add

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

Introduction

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

Prototype


public void add(XMLEventReader reader) throws XMLStreamException;

Source Link

Document

Adds an entire stream to an output stream, calls next() on the inputStream argument until hasNext() returns false This should be treated as a convenience method that will perform the following loop over all the events in an event reader and call add on each event.

Usage

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 .  j  a v a  2  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);

    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  ww w  .  j  a va  2 s  .com
}

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  ww . java2s. c  o  m*/
    });

    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  w w  w. j  a  v  a 2  s. c om
            writer.add(event);
        }
    }
    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);

    StartElement startElement = xmlEventFactory.createStartElement("", "", "My-list");
    writer.add(startElement);/*from  w  ww  .  j  av  a 2s.  co  m*/

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

    StartElement startElement = xmlEventFactory.createStartElement("", "", "My-list");
    writer.add(startElement);/*from  w w  w.  j  ava  2s.  c o  m*/

    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

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);/*  w  w w. j av  a  2 s  .  c  o  m*/
    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 {
    File source = new File(args[0]);
    File target = new File(source + ".new");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    InputStream in = new FileInputStream(source);
    XMLEventReader reader = inputFactory.createXMLEventReader(in);

    OutputStream out = new FileOutputStream(target);
    XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
    XMLEvent event;/* w  ww.  j  a v  a 2 s.c om*/

    boolean deleteSection = false;
    while (reader.hasNext()) {
        event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && event.asStartElement().getName().toString().equalsIgnoreCase("companies")) {
            deleteSection = true;
            continue;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (event.asEndElement().getName().toString().equalsIgnoreCase("companies"))) {
            deleteSection = false;
            continue;
        } else if (deleteSection) {
            continue;
        } else {
            writer.add(event);
        }
    }
}

From source file:Main.java

/**
 * Copy an XML event stream.// w  w  w . j  a va 2s. co m
 * @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:Main.java

/**
 * Describe <code>getInternalXML</code> method here.
 *
 * @param is an <code>InputStream</code> value
 * @param tagName a <code>String</code> value
 * @param namespace a <code>String</code> value
 * @return a <code>byte[]</code> value
 * @exception Exception if an error occurs
 *//*from  w  ww  . j a v  a  2s  .  c om*/
public static byte[] getInternalXML(InputStream is, String tagName, String namespace) throws Exception {

    if (namespace == null)
        namespace = "";

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

    // Loop over XML input stream and process events
    boolean bStartWritingEvents = false;

    XMLEventReader reader = inputFactory.createXMLEventReader(is);

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (checkNameAndNamespace(event, tagName, namespace) == true) {
            if (event.isStartElement() && !bStartWritingEvents) {
                writer.add(eventFactory.createStartDocument());
                bStartWritingEvents = true;
                continue;
            } else if (event.isEndElement()) {
                writer.add(eventFactory.createEndDocument());
                bStartWritingEvents = false;
                break;
            }
        }

        if (bStartWritingEvents) {
            writer.add(event);
        }
    }

    writer.close();
    reader.close();

    return bos.toByteArray();
}