Example usage for javax.xml.stream XMLStreamWriter writeEmptyElement

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

Introduction

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

Prototype

public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException;

Source Link

Document

Writes an empty element tag to the output

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.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 ww. j a v 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 .jav a  2 s.com
    writer.writeEndDocument();
    writer.flush();
    System.out.println();

}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0077.RegistrationCodec.java

@Override
public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException {
    xmlsw.writeStartElement("", "query", InBandRegistration.REGISTER);
    xmlsw.writeDefaultNamespace(InBandRegistration.REGISTER);

    xmlsw.writeEmptyElement(InBandRegistration.REGISTER, "username");
    xmlsw.writeEmptyElement(InBandRegistration.REGISTER, "password");

    xmlsw.writeEndElement();/*  ww w. j av a 2s. c  om*/
}

From source file:org.maodian.flyingcat.xmpp.state.DefaultElementVisitor.java

@Override
public State handleSASL(XmppContext ctx, Auth auth) throws XMLStreamException {
    ctx.login(auth.getAuthcid(), auth.getPassword());
    StringWriter writer = new StringWriter();
    XMLStreamWriter xmlsw = XMLOutputFactoryHolder.getXMLOutputFactory().createXMLStreamWriter(writer);
    xmlsw.setPrefix("", XmppNamespace.SASL);
    xmlsw.writeEmptyElement(XmppNamespace.SASL, "success");
    xmlsw.writeDefaultNamespace(XmppNamespace.SASL);
    xmlsw.writeEndDocument();//from  www  . j  av  a2  s. c o  m
    ctx.flush(writer.toString());
    return ctx.getGlobalContext().getAuthenticatedStreamState();
}

From source file:jodtemplate.pptx.io.xml.SlideXmlRelsWriter.java

@Override
public void write(final Resources resources, final Slide slide, final XMLOutputFactory xmlOutputFactory)
        throws XMLStreamException, IOException {
    final String slideXmlPath = FilenameUtils
            .normalize(slide.getPresentation().getFullPath() + slide.getRelationship().getTarget(), true);
    final String slideXmlRelsPath = Utils.getRelsPathNoPrefixSeparator(slideXmlPath);
    final Resource slideXmlRelsRes = resources.getResource(slideXmlRelsPath);

    try (final OutputStream os = slideXmlRelsRes.getOutputStream()) {
        os.write(//  ww  w  .j  a v  a 2s . c o m
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n".getBytes(CharEncoding.UTF_8));
        final XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(os);
        writer.writeStartElement(OOXMLDocument.RELATIONSHIPS_ELEMENT);
        writer.writeNamespace("", OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE);
        for (Relationship rel : slide.getOtherRelationships()) {
            writer.writeEmptyElement(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE,
                    OOXMLDocument.RELATIONSHIP_ELEMENT);
            writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE, OOXMLDocument.ID_ATTRIBUTE,
                    rel.getId());
            writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE, OOXMLDocument.TYPE_ATTRIBUTE,
                    rel.getType());
            writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE, OOXMLDocument.TARGET_ATTRIBUTE,
                    rel.getTarget());
            if (StringUtils.isNotBlank(rel.getTargetMode())) {
                writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE,
                        OOXMLDocument.TARGET_MODE_ATTRIBUTE, rel.getTargetMode());
            }
            writer.flush();
        }
        writer.writeEndElement();
        writer.writeEndDocument();

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

From source file:de.escidoc.core.common.util.xml.XmlUtility.java

/**
 * Adds a new element to the provided {@code XMLStreamWriter} object containing a {@code String} value.
 *
 * @param writer         The {@code XMLStreamWriter} object to add the element to.
 * @param elementName    The name of the new element.
 * @param elementContent The {@code String} that shall be set as the value of the new element.
 * @param namespaceUri   The namespace URI of the new element.
 * @param createEmpty    Flag indicating if a an empty element shall be created if the provided data is
 *                       {@code null} ( {@code true} ), or if the element shall not be created (
 *                       {@code false} ).
 * @throws XMLStreamException Thrown in case of an xml stream error.
 *//*from w  w w .  ja va2  s  . c om*/
public static void addElement(final XMLStreamWriter writer, final String elementName,
        final String elementContent, final String namespaceUri, final boolean createEmpty)
        throws XMLStreamException {
    if (elementContent == null) {
        if (createEmpty) {
            writer.writeEmptyElement(namespaceUri, elementName);
        }
    } else {
        writer.writeStartElement(namespaceUri, elementName);
        writer.writeCharacters(elementContent);
        writer.writeEndElement();
    }
}

From source file:de.escidoc.core.common.util.xml.XmlUtility.java

/**
 * Adds a new element to the provided {@code XMLStreamWriter} object containing a date value.
 *
 * @param writer         The {@code XMLStreamWriter} object to add the element to.
 * @param elementName    The name of the new element.
 * @param elementContent The {@code Date} that shall be set as the value of the new element.
 * @param namespaceUri   The namespace URI of the new element.
 * @param createEmpty    Flag indicating if a an empty element shall be created if the provided data is
 *                       {@code null} ( {@code true} ), or if the element shall not be created (
 *                       {@code false} ).
 * @throws XMLStreamException Thrown in case of an xml stream error.
 *//*  www. j a  va 2  s.c  om*/
public static void addElement(final XMLStreamWriter writer, final String elementName,
        final DateTime elementContent, final String namespaceUri, final boolean createEmpty)
        throws XMLStreamException {

    if (elementContent == null) {
        if (createEmpty) {
            writer.writeEmptyElement(namespaceUri, elementName);
        }
    } else {
        writer.writeStartElement(namespaceUri, elementName);
        writer.writeCharacters(elementContent.withZone(DateTimeZone.UTC).toString(Constants.TIMESTAMP_FORMAT));
        writer.writeEndElement();
    }
}

From source file:de.shadowhunt.subversion.internal.PropertiesUpdateOperation.java

@Override
protected HttpUriRequest createRequest() {
    final URI uri = URIUtils.createURI(repository, resource);
    final DavTemplateRequest request = new DavTemplateRequest("PROPPATCH", uri);

    if (lockToken != null) {
        request.addHeader("If", "<" + uri + "> (<" + lockToken + ">)");
    }//from   w  w w.ja  va  2 s  .  c o  m

    final Writer body = new StringBuilderWriter();
    try {
        final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(body);
        writer.writeStartDocument(XmlConstants.ENCODING, XmlConstants.VERSION_1_0);
        writer.writeStartElement("propertyupdate");
        writer.writeDefaultNamespace(XmlConstants.DAV_NAMESPACE);
        writer.setPrefix(XmlConstants.SUBVERSION_CUSTOM_PREFIX, XmlConstants.SUBVERSION_CUSTOM_NAMESPACE);
        writer.writeNamespace(XmlConstants.SUBVERSION_CUSTOM_PREFIX, XmlConstants.SUBVERSION_CUSTOM_NAMESPACE);
        writer.setPrefix(XmlConstants.SUBVERSION_DAV_PREFIX, XmlConstants.SUBVERSION_DAV_NAMESPACE);
        writer.writeNamespace(XmlConstants.SUBVERSION_DAV_PREFIX, XmlConstants.SUBVERSION_DAV_NAMESPACE);
        writer.setPrefix(XmlConstants.SUBVERSION_SVN_PREFIX, XmlConstants.SUBVERSION_SVN_NAMESPACE);
        writer.writeNamespace(XmlConstants.SUBVERSION_SVN_PREFIX, XmlConstants.SUBVERSION_SVN_NAMESPACE);
        writer.writeStartElement(type.action);
        writer.writeStartElement("prop");
        for (final ResourceProperty property : properties) {
            final String prefix = property.getType().getPrefix();
            if (type == Type.SET) {
                writer.writeStartElement(prefix, ResourcePropertyUtils.escapedKeyNameXml(property.getName()));
                writer.writeCharacters(property.getValue());
                writer.writeEndElement();
            } else {
                writer.writeEmptyElement(prefix, property.getName());
            }
        }
        writer.writeEndElement(); // prop
        writer.writeEndElement(); // set || delete
        writer.writeEndElement(); // propertyupdate
        writer.writeEndDocument();
        writer.close();
    } catch (final XMLStreamException e) {
        throw new SubversionException("could not create request body", e);
    }

    final String bodyWithMakers = body.toString();
    final String bodyWithoutMakers = ResourcePropertyUtils.filterMarker(bodyWithMakers);
    request.setEntity(new StringEntity(bodyWithoutMakers, CONTENT_TYPE_XML));
    return request;
}

From source file:nl.armatiek.xslweb.serializer.RequestSerializer.java

private void dataElement(XMLStreamWriter xsw, String uri, String localName, String text)
        throws XMLStreamException {
    if (text == null) {
        return;/*from   ww w . j  a va2 s  . c o  m*/
    }
    if (text.equals("")) {
        xsw.writeEmptyElement(uri, localName);
    } else {
        xsw.writeStartElement(uri, localName);
        xsw.writeCharacters(text);
        xsw.writeEndElement();
    }
}

From source file:nl.nn.adapterframework.soap.Wsdl.java

protected void message(XMLStreamWriter w, String root, Collection<QName> parts)
        throws XMLStreamException, IOException {
    if (!parts.isEmpty()) {
        w.writeStartElement(WSDL_NAMESPACE, "message");
        w.writeAttribute("name", "Message_" + root);
        {/*from   w ww .  j a v  a  2  s  .  c  om*/
            for (QName part : parts) {
                w.writeEmptyElement(WSDL_NAMESPACE, "part");
                w.writeAttribute("name", "Part_" + part.getLocalPart());
                String type = part.getPrefix() + ":" + part.getLocalPart();
                w.writeAttribute("element", type);
            }
        }
        w.writeEndElement();
    }
}