Example usage for javax.xml.stream XMLStreamWriter writeComment

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

Introduction

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

Prototype

public void writeComment(String data) throws XMLStreamException;

Source Link

Document

Writes an xml comment with the data enclosed

Usage

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 www  .j  a v a  2s .  c  om*/
    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 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  .  c o m*/
    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

/**
 * @param comment/*w w  w . j a  v a  2s .c o m*/
 * @param xmlsw
 * @throws XMLStreamException
 */
public static void writeComment(String comment, XMLStreamWriter xmlsw) throws XMLStreamException {
    xmlsw.writeComment(comment);
}

From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java

public static void write(List<Scale> modeList, String fileName) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;

    // <mode>
    // <name>Peruvian tritonic 2</name>
    // <interval>3</interval>
    // <interval>4</interval>
    // <interval>5</interval>
    // </mode>

    String ns = "http://rockhoppertech.com/modes-1.0";

    // StringWriter sw = new StringWriter();
    try {/*  ww  w . j  a  va  2s.com*/
        xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
        // xtw = xof.createXMLStreamWriter(sw);
        xtw.writeComment("all elements here are explicitly in the mode namespace");
        xtw.writeStartDocument("utf-8", "1.0");
        xtw.setPrefix("modes", ns);
        xtw.writeStartElement(ns, "modes");
        xtw.writeNamespace("modes", ns);

        for (Scale scale : modeList) {
            // xtw.writeStartElement(ns, "mode");
            xtw.writeStartElement("mode");

            // xtw.writeStartElement(ns, "name");
            xtw.writeStartElement("name");
            xtw.writeCharacters(scale.getName());
            xtw.writeEndElement();

            // xtw.writeStartElement(ns, "intervals");
            // xtw.writeStartElement(ns, "interval");
            xtw.writeStartElement("intervals");
            int[] intervals = scale.getIntervals();
            for (int i = 0; i < intervals.length; i++) {
                xtw.writeStartElement("interval");
                xtw.writeCharacters("" + intervals[i]);
                xtw.writeEndElement();
            }
            xtw.writeEndElement(); // intervals

            xtw.writeEndElement(); // mode
        }

        xtw.writeEndElement(); // modes
        xtw.writeEndDocument();
        xtw.flush();
        xtw.close();
        // System.err.println(sw.toString());

    } catch (XMLStreamException | IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        e.printStackTrace();
    }

}

From source file:microsoft.exchange.webservices.data.core.EwsServiceXmlWriter.java

/**
 * @param xmlNode XML node//from   w  ww .  ja v a 2s . com
 * @param xmlStreamWriter XML stream writer
 * @throws XMLStreamException the XML stream exception
 */
public static void writeNode(Node xmlNode, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    if (xmlNode instanceof Element) {
        addElement((Element) xmlNode, xmlStreamWriter);
    } else if (xmlNode instanceof Text) {
        xmlStreamWriter.writeCharacters(xmlNode.getNodeValue());
    } else if (xmlNode instanceof CDATASection) {
        xmlStreamWriter.writeCData(((CDATASection) xmlNode).getData());
    } else if (xmlNode instanceof Comment) {
        xmlStreamWriter.writeComment(((Comment) xmlNode).getData());
    } else if (xmlNode instanceof EntityReference) {
        xmlStreamWriter.writeEntityRef(xmlNode.getNodeValue());
    } else if (xmlNode instanceof ProcessingInstruction) {
        ProcessingInstruction procInst = (ProcessingInstruction) xmlNode;
        xmlStreamWriter.writeProcessingInstruction(procInst.getTarget(), procInst.getData());
    } else if (xmlNode instanceof Document) {
        writeToDocument((Document) xmlNode, xmlStreamWriter);
    }
}

From source file:com.github.lindenb.jvarkit.util.command.Command.java

protected void writeHtmlDoc(final XMLStreamWriter w) throws XMLStreamException {
    w.writeStartElement("div");
    w.writeComment("no doc available for " + getName());
    w.writeEndElement();/*from  ww  w  .  j  a v  a2  s .c o m*/
}

From source file:XmlReaderToWriter.java

public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null)
                writer.writeStartElement(prefix, localName, namespaceURI);
            else/*from   w  w w . j  a v a 2s. co m*/
                writer.writeStartElement(namespaceURI, localName);
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            writer.writeNamespace(xmlr.getNamespacePrefix(i), xmlr.getNamespaceURI(i));
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            String attUri = xmlr.getAttributeNamespace(i);
            if (attUri != null)
                writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            else
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength());
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;

    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null)
            writer.writeStartDocument(encoding, version);
        else if (version != null)
            writer.writeStartDocument(xmlr.getVersion());
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:ca.uhn.fhir.parser.XmlParser.java

private void writeCommentsPre(XMLStreamWriter theEventWriter, IBase theElement) throws XMLStreamException {
    if (theElement != null && theElement.hasFormatComment()) {
        for (String next : theElement.getFormatCommentsPre()) {
            if (isNotBlank(next)) {
                theEventWriter.writeComment(next);
            }/*from  w w  w  . j av a  2 s  .  c  om*/
        }
    }
}

From source file:ca.uhn.fhir.parser.XmlParser.java

private void writeCommentsPost(XMLStreamWriter theEventWriter, IBase theElement) throws XMLStreamException {
    if (theElement != null && theElement.hasFormatComment()) {
        for (String next : theElement.getFormatCommentsPost()) {
            if (isNotBlank(next)) {
                theEventWriter.writeComment(next);
            }/*from w ww . j  a va  2  s. c o  m*/
        }
    }
}

From source file:Main.java

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//*from  w  ww .j  a v  a 2 s.co  m*/
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null) {
                writer.writeStartElement(prefix, localName, namespaceURI);
            } else {
                writer.writeStartElement(namespaceURI, localName);
            }
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            String prefix = xmlr.getNamespacePrefix(i);
            if (prefix == null) {
                writer.writeDefaultNamespace(xmlr.getNamespaceURI(i));
            } else {
                writer.writeNamespace(prefix, xmlr.getNamespaceURI(i));
            }
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            final String attUri = xmlr.getAttributeNamespace(i);

            if (attUri != null && attUri.length() > 0) {
                final String prefix = xmlr.getAttributePrefix(i);
                if (prefix != null) {
                    writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i),
                            xmlr.getAttributeValue(i));
                } else {
                    writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                }
            } else {
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            }

        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        char[] text = new char[xmlr.getTextLength()];
        xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength());
        writer.writeCharacters(text, 0, text.length);
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;
    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null) {
            writer.writeStartDocument(encoding, version);
        } else if (version != null) {
            writer.writeStartDocument(xmlr.getVersion());
        }
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}