Example usage for javax.xml.stream XMLStreamWriter writeStartDocument

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

Introduction

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

Prototype

public void writeStartDocument(String version) throws XMLStreamException;

Source Link

Document

Write the XML Declaration.

Usage

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();/*w ww.java2  s .c o  m*/
    writer.flush();
}

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();/*  www. j  av a2s  . c o m*/
    writer.writeEndDocument();
    writer.flush();
}

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 va2 s  .c om
    writer.writeEndDocument();
    writer.flush();
    System.out.println();

}

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.writeStartElement("addresses");
    writer.writeStartElement("address");
    writer.writeAttribute("type", "work");
    writer.writeStartElement("street");
    writer.writeCharacters("1111 Ave");
    writer.writeComment("comments");
    writer.writeEndElement();/*  w  w w. ja  va 2  s  .  com*/
    writer.writeEmptyElement("inner");
    writer.flush();
    System.out.println();
    writer.writeAttribute("otherAttribute", "true");
    writer.flush();
    System.out.println();
    writer.writeEndElement();
    writer.writeEndElement();
    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();/*w ww  .  j  a  v a2s . c o m*/

    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:com.adobe.acs.commons.wcm.impl.SiteMapServlet.java

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType(request.getResponseContentType());
    if (StringUtils.isNotEmpty(this.characterEncoding)) {
        response.setCharacterEncoding(characterEncoding);
    }/*from  w ww .  j  a v a2s. co  m*/
    ResourceResolver resourceResolver = request.getResourceResolver();
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    Page page = pageManager.getContainingPage(request.getResource());

    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    try {
        XMLStreamWriter stream = outputFactory.createXMLStreamWriter(response.getWriter());
        stream.writeStartDocument("1.0");

        stream.writeStartElement("", "urlset", NS);
        stream.writeNamespace("", NS);

        // first do the current page
        write(page, stream, resourceResolver);

        for (Iterator<Page> children = page.listChildren(new PageFilter(false, true), true); children
                .hasNext();) {
            write(children.next(), stream, resourceResolver);
        }

        if (damAssetTypes.size() > 0 && damAssetProperty.length() > 0) {
            for (Resource assetFolder : getAssetFolders(page, resourceResolver)) {
                writeAssets(stream, assetFolder, resourceResolver);
            }
        }

        stream.writeEndElement();

        stream.writeEndDocument();
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Writes a Salt project to the file given by {@link #getPath()}.
 * // w w  w . ja v  a  2 s  .c o m
 * @param project
 *            the Salt project to be written
 */
public void writeSaltProject(SaltProject project) {
    XMLStreamWriter xml = null;
    try (OutputStream output = new FileOutputStream(path)) {
        xml = xmlFactory.createXMLStreamWriter(output, "UTF-8");
        xml.writeStartDocument("1.0");
        if (isPrettyPrint) {
            xml.writeCharacters("\n");
        }
        writeSaltProject(xml, project);

        xml.writeEndDocument();

    } catch (XMLStreamException | IOException e) {
        throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr() + "'. ", e);
    } finally {
        if (xml != null) {
            try {
                xml.flush();
                xml.close();
            } catch (XMLStreamException e) {
                throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr()
                        + "', because the opened stream is not closable. ", e);
            }
        }
    }
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Writes a corpus graph to the xml stream
 * /*from   ww  w  . ja v  a  2  s  . com*/
 * @param graph
 *            the corpus graph to be written
 * @param embedded
 *            determines whether this corpus graph is part of a saltProject
 * @param xml
 *            xml stream to write corpus graph to, if the passed one is
 *            null, a new one will be created
 */
public void writeCorpusGraph(XMLStreamWriter xml, SCorpusGraph graph, boolean embedded) {
    try {
        if (!embedded) {
            xml.writeStartDocument("1.0");
            if (isPrettyPrint) {
                xml.writeCharacters("\n");
                xml.writeStartElement(NS_SALTCOMMON, TAG_SCORPUSGRAPH, NS_VALUE_SALTCOMMON);
                xml.writeNamespace(NS_SCORPUSSTRUCTURE, NS_VALUE_SCORPUSSTRUCTURE);
                xml.writeNamespace(NS_XMI, NS_VALUE_XMI);
                xml.writeNamespace(NS_XSI, NS_VALUE_XSI);
                xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE);
                xml.writeNamespace(NS_SALTCOMMON, NS_VALUE_SALTCOMMON);
                xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0");
            }
        } else {
            xml.writeStartElement(TAG_SCORPUSGRAPH);
        }

        // write all labels
        if (graph.getLabels() != null) {
            Iterator<Label> labelIt = graph.getLabels().iterator();
            while (labelIt.hasNext()) {
                if (isPrettyPrint) {
                    xml.writeCharacters("\n");
                    xml.writeCharacters("\t");
                }
                writeLabel(xml, labelIt.next());
            }
        }
        // stores the position of a single node in the list of nodes to
        // refer them in a relation
        Map<SNode, Integer> nodePositions = new HashMap<>();

        // write all nodes
        if (graph.getNodes() != null) {
            Iterator<SNode> nodeIt = graph.getNodes().iterator();
            Integer position = 0;
            while (nodeIt.hasNext()) {
                SNode node = nodeIt.next();
                writeNode(xml, node, null);
                nodePositions.put(node, position);
                position++;
            }
        }

        // write all relations
        if (graph.getRelations() != null) {
            Iterator<SRelation<SNode, SNode>> relIt = graph.getRelations().iterator();
            while (relIt.hasNext()) {
                SRelation<SNode, SNode> rel = relIt.next();
                writeRelation(xml, rel, nodePositions, null);
            }
        }
        if (isPrettyPrint) {
            xml.writeCharacters("\n");
            xml.writeCharacters("\t");
        }
        xml.writeEndElement();
    } catch (XMLStreamException e) {
        throw new SaltResourceException(
                "Cannot store salt project to file '" + getLocationStr() + "'. " + e.getMessage(), e);
    } finally {
        if (!embedded) {
            if (xml != null) {
                try {
                    xml.flush();
                    xml.close();
                } catch (XMLStreamException e) {
                    throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr()
                            + "', because the opened stream is not closable. ", e);
                }
            }
        }
    }
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Writes a document graph to the file given by {@link #getPath()}.
 * /*from   ww  w.  j ava 2  s.  c o m*/
 * @param graph
 */
public void writeDocumentGraph(SDocumentGraph graph) {
    XMLStreamWriter xml = null;
    try (OutputStream output = new FileOutputStream(path)) {
        xml = xmlFactory.createXMLStreamWriter(output, "UTF-8");

        xml.writeStartDocument("1.0");
        if (isPrettyPrint) {
            xml.writeCharacters("\n");
        }

        writeDocumentGraph(xml, graph);

        xml.writeEndDocument();
    } catch (XMLStreamException | IOException e) {
        throw new SaltResourceException("Cannot store document graph to file '" + getLocationStr() + "'. ", e);
    } finally {
        if (xml != null) {
            try {
                xml.flush();
                xml.close();
            } catch (XMLStreamException e) {
                throw new SaltResourceException("Cannot store document graph to file '" + getLocationStr()
                        + "', because the opened stream is not closable. ", e);
            }
        }
    }
}