Example usage for javax.xml.stream XMLStreamWriter close

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

Introduction

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

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Close this writer and free any resources associated with the writer.

Usage

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 ww w.j  a va 2 s  .  c o m*/
    xsw.writeStartElement("response");
    xsw.writeStartElement("message");
    xsw.writeCharacters("1 < 2");
    xsw.writeEndDocument();

    xsw.close();
}

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();//ww  w.  j a v a2  s  .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 {
    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();/* w  w  w .j  ava  2 s  .c  o  m*/
    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();
}

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 .  j  ava 2 s  .com

    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:CursorWriter.java

public static void main(String[] args) throws Exception {
    String fileName = "yourXML.xml";
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;
    xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
    xtw.writeComment("all elements here are explicitly in the HTML namespace");
    xtw.writeStartDocument("utf-8", "1.0");
    xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html");
    xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title");
    xtw.writeCharacters("character");
    xtw.writeEndElement();/*from w w w .ja  v  a  2 s . com*/
    xtw.writeEndElement();
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p");
    xtw.writeCharacters("another character");
    xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a");
    xtw.writeAttribute("href", "http://www.java2s.com");
    xtw.writeCharacters("here");
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndElement();
    xtw.writeEndDocument();
    xtw.flush();
    xtw.close();
    System.out.println("Done");
}

From source file:Main.java

public static void endDocument(XMLStreamWriter writer) throws XMLStreamException {
    endElement(writer);//from   w  w  w  .  jav  a  2  s  .c om
    writer.close();
}

From source file:Main.java

/**
 * Ends a XML document.// w  w  w.j  av a  2s  .  c  o m
 */
public static void endXmlDocument(XMLStreamWriter writer) throws XMLStreamException {
    assert writer != null;

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

From source file:Main.java

public static String convertBean2Xml(Object obj) throws IOException {
    String result = null;//from ww  w .  ja  v a2 s  . co 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:com.hazelcast.stabilizer.Utils.java

public static void closeQuietly(XMLStreamWriter c) {
    if (c == null)
        return;/*from w w w  . j a  v  a2s . c  om*/
    try {
        c.close();
    } catch (XMLStreamException ignore) {
    }
}

From source file:com.norconex.jefmon.model.ConfigurationDAO.java

public static void saveConfig(JEFMonConfig config) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Saving JEF config to: " + CONFIG_FILE);
    }/*from   w  w w  . j  a  va2s  .  c o m*/

    if (!CONFIG_FILE.exists()) {
        File configDir = new File(FilenameUtils.getFullPath(CONFIG_FILE.getAbsolutePath()));
        if (!configDir.exists()) {
            LOG.debug("Creating JEF Monitor config directory for: " + CONFIG_FILE);
            configDir.mkdirs();
        }
    }

    OutputStream out = new FileOutputStream(CONFIG_FILE);
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter xml = factory.createXMLStreamWriter(out);
        xml.writeStartDocument();
        xml.writeStartElement("jefmon-config");

        xml.writeStartElement("instance-name");
        xml.writeCharacters(config.getInstanceName());
        xml.writeEndElement();

        xml.writeStartElement("default-refresh-interval");
        xml.writeCharacters(Integer.toString(config.getDefaultRefreshInterval()));
        xml.writeEndElement();

        saveRemoteUrls(xml, config.getRemoteInstanceUrls());
        saveMonitoredPaths(xml, config.getMonitoredPaths());
        saveJobActions(xml, config.getJobActions());

        xml.writeEndElement();
        xml.writeEndDocument();
        xml.flush();
        xml.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
    out.close();
}