Example usage for javax.xml.stream XMLStreamWriter writeAttribute

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

Introduction

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

Prototype

public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException;

Source Link

Document

Writes an attribute to the output stream

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  w  w.  j  a va  2s.c om
    writer.writeEndDocument();
    writer.flush();
}

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 ww w  . jav  a2  s.  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:org.deegree.services.ows.OWS110ExceptionReportSerializer.java

@Override
public void serializeExceptionToXML(XMLStreamWriter writer, OWSException ex) throws XMLStreamException {
    if (ex == null || writer == null) {
        return;//from w  ww.jav  a2  s. com
    }
    writer.writeStartElement("ows", "ExceptionReport", OWS_NS);
    writer.writeNamespace("ows", OWS_NS);
    writer.writeNamespace("xsi", XSINS);
    writer.writeAttribute(XSINS, "schemaLocation", OWS_NS + " " + OWS_SCHEMA);
    writer.writeAttribute("version", "" + version);
    writer.writeStartElement(OWS_NS, "Exception");
    writer.writeAttribute("exceptionCode", ex.getExceptionCode());
    if (ex.getLocator() != null && !"".equals(ex.getLocator().trim())) {
        writer.writeAttribute("locator", ex.getLocator());
    }
    String message = ex.getMessage();
    if (message != null) {
        writer.writeStartElement(OWS_NS, "ExceptionText");
        writer.writeCharacters(message);
        writer.writeEndElement();
    }
    writer.writeEndElement(); // Exception
    writer.writeEndElement(); // ExceptionReport
}

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 a2  s.  co 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.qucosa.webapi.v1.DocumentResource.java

private String getDocumentCreatedResponse(String id) throws XMLStreamException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeStartElement("Opus_Document");
    w.writeNamespace(XLINK_NAMESPACE_PREFIX, XLINK_NAMESPACE);
    w.writeAttribute(XLINK_NAMESPACE, "href", getHrefLink(id));
    w.writeAttribute("id", id);
    w.writeEndElement();/*from w ww .j a  v a  2 s.c o m*/
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    return sw.toString();
}

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

/**
 * Adds the "xml:base" attribute to the provided {@code XMLStreamWriter}.<br> The value of the attribute is set
 * to the value of the configuration property {@code escidoc.baseurl}.
 *
 * @param writer The {@code XMLStreamWriter} object to add the attribute to.
 * @throws IOException        Thrown if the base url cannot be determined.
 * @throws XMLStreamException Thrown in case of an xml stream error.
 *///from  ww  w. jav a2 s. c o m
public static void addXmlBaseAttribute(final XMLStreamWriter writer) throws XMLStreamException {

    writer.writeAttribute(Constants.XML_NS_URI, "base",
            EscidocConfiguration.getInstance().get(EscidocConfiguration.ESCIDOC_CORE_BASEURL));
}

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

/**
 * Adds the xlink attributes to the provided {@code Element}.<br> The attribute "xlink:type" is set to
 * "simple", the attributes "xlink:title" and "xlink:href" to the respective provided values.<br> If the provided
 * title is {@code null}. the title attribute is skipped.
 *
 * @param writer     The {@code XMLStreamWriter} object to add the attributes to.
 * @param xlinkTitle The title of the xlink.
 * @param xlinkHref  The href of the xlink.
 * @throws XMLStreamException Thrown in case of an xml stream error.
 *//*from w w  w  .j  a  v  a2 s. com*/
public static void addXlinkAttributes(final XMLStreamWriter writer, final String xlinkTitle,
        final String xlinkHref) throws XMLStreamException {

    writer.writeAttribute(Constants.XLINK_NS_URI, "type", "simple");
    if (xlinkTitle != null) {
        writer.writeAttribute(Constants.XLINK_NS_URI, "title", xlinkTitle);
    }
    writer.writeAttribute(Constants.XLINK_NS_URI, "href", xlinkHref);
}

From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java

/**
 * Defines all attributes of type {@link DoubleDomain}, {@link LongDomain},
 * {@link MapDomain} or {@link CollectionDomain} which are used in the
 * {@link SchemaGraph} i.e. contained in
 * {@link SchemaGraph2XMI#typesToBeDeclaredAtTheEnd}. These definitions are
 * created in a new UML package, called <code>PrimitiveTypes</code>. This is
 * necessary because those {@link Domain}s are not UML primitive types and
 * are not represented by an own <code>packagedElement</code> in the XMI
 * file.// w  w  w .j  a  v a  2  s  .c  o m
 * 
 * @param writer
 *            {@link XMLStreamWriter} of the current XMI file
 * @throws XMLStreamException
 */
private void createTypes(XMLStreamWriter writer) throws XMLStreamException {
    // start packagedElement
    writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE,
            XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_PACKAGE);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID,
            XMIConstants4SchemaGraph2XMI.PACKAGE_PRIMITIVETYPES_NAME);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME,
            XMIConstants4SchemaGraph2XMI.PACKAGE_PRIMITIVETYPES_NAME);

    // create entries for domains, which are not defined
    for (Domain domain : typesToBeDeclaredAtTheEnd) {
        writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT);
        writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
                XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE,
                XMIConstants4SchemaGraph2XMI.TYPE_VALUE_PRIMITIVETYPE);
        writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
                XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID,
                domain.get_qualifiedName().replaceAll("\\s", "").replaceAll("<", "_").replaceAll(">", "_"));
        writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME,
                extractSimpleName(domain.get_qualifiedName()));
    }

    // end packagedElement
    writer.writeEndElement();
}

From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java

/**
 * Creates the profileApplication tag at the end of the model tag.
 * // w  w  w .j a v  a  2  s .co m
 * @param writer
 *            {@link XMLStreamWriter} of the current XMI file
 * @throws XMLStreamException
 */
private void createProfileApplication(XMLStreamWriter writer) throws XMLStreamException {
    // start profileApplication
    writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_PROFILEAPPLICATION);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE,
            XMIConstants4SchemaGraph2XMI.PROFILEAPPLICATION_TYPE_VALUE);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID,
            XMIConstants4SchemaGraph2XMI.TAG_PROFILEAPPLICATION + System.currentTimeMillis());

    // create content
    createExtension(writer, null, null);

    // create appliedProfile
    writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_APPLIEDPROFILE);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE,
            XMIConstants4SchemaGraph2XMI.APPLIEDPROFILE_TYPE_VALUE);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_HREF,
            XMIConstants4SchemaGraph2XMI.APPLIEDPROFILE_HREF_VALUE);

    // end profileApplication
    writer.writeEndElement();
}

From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java

/**
 * Creates the representation of the {@link RecordDomain}
 * <code>recordDomain</code>. This representation consists of an UML class
 * with stereotype <code>&lt;&lt;record&gt;&gt;</code>. The components of
 * <code>recordDomain</code> are represented as attributes of the generated
 * UML class. Further more all {@link Comment}s attached to
 * <code>recordDomain</code> are created.
 * //w w w. ja  va  2s.  c o m
 * @param writer
 *            {@link XMLStreamWriter} of the current XMI file
 * @param recordDomain
 *            {@link RecordDomain} the current {@link RecordDomain}
 * @throws XMLStreamException
 */
private void createRecordDomain(XMLStreamWriter writer, RecordDomain recordDomain) throws XMLStreamException {
    // start packagedElement
    writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE,
            XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_CLASS);
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI,
            XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, recordDomain.get_qualifiedName());
    writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME,
            extractSimpleName(recordDomain.get_qualifiedName()));

    // create stereotype <<record>>
    createExtension(writer, recordDomain, "record");

    // create comments
    createComments(writer, recordDomain);

    // create attributes
    for (HasRecordDomainComponent hrdc : recordDomain
            .getHasRecordDomainComponentIncidences(EdgeDirection.OUT)) {
        createAttribute(writer, hrdc.get_name(), null, (Domain) hrdc.getThat(),
                recordDomain.get_qualifiedName() + "_" + hrdc.get_name());
    }

    // end packagededElement
    writer.writeEndElement();
}