Example usage for javax.xml.stream XMLStreamWriter writeEndDocument

List of usage examples for javax.xml.stream XMLStreamWriter writeEndDocument

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeEndDocument.

Prototype

public void writeEndDocument() throws XMLStreamException;

Source Link

Document

Closes any start tags and writes corresponding end tags.

Usage

From source file:EmptyElement.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeEmptyElement("empty");
    writer.writeAttribute("attribute", "true");
    writer.writeEndDocument();
    writer.flush(); // write /> to the console
}

From source file:Main.java

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

    StringWriter sw = new StringWriter();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(sw);
    xsw.writeStartDocument();/*w  ww .  j  a  v  a 2s .c o  m*/
    xsw.writeStartElement("foo");
    xsw.writeCharacters("<>\"&'");
    xsw.writeEndDocument();

    String xml = sw.toString();
    System.out.println(xml);

    // READ THE XML
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
    xsr.nextTag(); // Advance to "foo" element
    System.out.println(xsr.getElementText());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

    xsw.writeStartDocument();//from   www.  j  a  v  a 2  s.c om
    xsw.writeStartElement("response");
    xsw.writeStartElement("message");
    xsw.writeCharacters("1 < 2");
    xsw.writeEndDocument();

    xsw.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartDocument("1.0");
    writer.writeStartElement("person");
    writer.writeStartElement("name");
    writer.writeStartElement("fname");
    writer.writeCharacters("AAA");

    writer.writeEndDocument();
    writer.flush();//from w ww .  ja  va2  s .com
}

From source file:MainClass.java

public static void main(String[] args) throws XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartDocument("1.0");

    writer.writeStartElement("http://www.t.com/f", "sample");

    writer.writeAttribute("attribute", "true");
    writer.writeAttribute("http://www.t.com/f", "attribute2", "false");
    writer.writeCharacters("some text");
    writer.writeCData("<test>");
    writer.writeEndElement();// ww  w  . j a v a  2s .c  om
    writer.writeEndDocument();
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    OutputStream stream = System.out;

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(stream);

    xsw.writeStartDocument();/*  w  ww . ja  va 2s  .  co  m*/
    xsw.writeStartElement("foo");
    xsw.writeStartElement("bar");

    xsw.writeCharacters("");
    xsw.flush();

    OutputStreamWriter osw = new OutputStreamWriter(stream);
    osw.write("<baz>Hello World<baz>");
    osw.flush();

    xsw.writeEndElement();
    xsw.writeEndElement();
    xsw.writeEndDocument();
    xsw.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartDocument("1.0");
    writer.writeCharacters("\n");
    writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1");
    writer.writeNamespace("ns1", "http://www.e.com/ns1");

    writer.writeEmptyElement("http://www.e.com/ns1", "inner1");
    writer.writeAttribute("otherAttribute", "true");
    writer.writeEndElement();//from   ww  w .  j  a v  a2 s .c  om
    writer.writeEndDocument();
    writer.flush();
    System.out.println();

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
    writer.writeStartElement("ns1", "sample", "http://www.e.com/ns1");
    writer.writeNamespace("ns1", "http://www.e.com/ns1");

    writer.writeAttribute("http://www.e.com/ns2", "attribute", "true");
    writer.writeEmptyElement("http://www.e.com/ns1", "inner");
    writer.writeEmptyElement("ns2", "inner", "http://www.e.com/ns2");
    writer.writeEndElement();/*from  w w w. j ava 2 s  .c  o m*/
    writer.writeEndDocument();
    writer.flush();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(System.out);

    writer.writeStartDocument("1.0");

    writer.writeStartElement("catalog");

    writer.writeStartElement("book");

    writer.writeAttribute("id", "1");

    writer.writeStartElement("code");
    writer.writeCharacters("I01");
    writer.writeEndElement();//from w w  w .  jav  a 2s .c  om

    writer.writeStartElement("title");
    writer.writeCharacters("This is the title");
    writer.writeEndElement();

    writer.writeStartElement("price");
    writer.writeCharacters("$2.95");
    writer.writeEndElement();

    writer.writeEndDocument();

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
    writer.setDefaultNamespace("http://www.java2s.com");

    JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    writer.writeStartDocument();//from ww w .  ja va 2 s. c  om
    writer.writeStartElement("http://www.java2s.com", "Import");
    writer.writeNamespace("", "http://www.java2s.com");
    writer.writeStartElement("WorkSets");

    m.marshal(new WorkSet(), writer);
    m.marshal(new WorkSet(), writer);

    writer.writeEndDocument();
    writer.close();
}