List of usage examples for javax.xml.stream XMLStreamWriter writeStartElement
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException;
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();/* ww w. j av a 2 s .co 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();// www.j av a 2 s.c o m writer.writeEndDocument(); writer.flush(); System.out.println(); }
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// w w w . j a va2s . c o 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:Main.java
/** * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter *///from w ww . j a v a 2s. 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; } }
From source file:org.elasticsearch.discovery.ec2.Ec2DiscoveryClusterFormationTests.java
/** * Creates mock EC2 endpoint providing the list of started nodes to the DescribeInstances API call *//* w ww .j ava 2 s . c om*/ @BeforeClass public static void startHttpd() throws Exception { logDir = createTempDir(); httpServer = MockHttpServer .createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); httpServer.createContext("/", (s) -> { Headers headers = s.getResponseHeaders(); headers.add("Content-Type", "text/xml; charset=UTF-8"); String action = null; for (NameValuePair parse : URLEncodedUtils.parse(IOUtils.toString(s.getRequestBody()), StandardCharsets.UTF_8)) { if ("Action".equals(parse.getName())) { action = parse.getValue(); break; } } assertThat(action, equalTo("DescribeInstances")); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory(); xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); StringWriter out = new StringWriter(); XMLStreamWriter sw; try { sw = xmlOutputFactory.createXMLStreamWriter(out); sw.writeStartDocument(); String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/"; sw.setDefaultNamespace(namespace); sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace); { sw.writeStartElement("requestId"); sw.writeCharacters(UUID.randomUUID().toString()); sw.writeEndElement(); sw.writeStartElement("reservationSet"); { Path[] files = FileSystemUtils.files(logDir); for (int i = 0; i < files.length; i++) { Path resolve = files[i].resolve("transport.ports"); if (Files.exists(resolve)) { List<String> addresses = Files.readAllLines(resolve); Collections.shuffle(addresses, random()); sw.writeStartElement("item"); { sw.writeStartElement("reservationId"); sw.writeCharacters(UUID.randomUUID().toString()); sw.writeEndElement(); sw.writeStartElement("instancesSet"); { sw.writeStartElement("item"); { sw.writeStartElement("instanceId"); sw.writeCharacters(UUID.randomUUID().toString()); sw.writeEndElement(); sw.writeStartElement("imageId"); sw.writeCharacters(UUID.randomUUID().toString()); sw.writeEndElement(); sw.writeStartElement("instanceState"); { sw.writeStartElement("code"); sw.writeCharacters("16"); sw.writeEndElement(); sw.writeStartElement("name"); sw.writeCharacters("running"); sw.writeEndElement(); } sw.writeEndElement(); sw.writeStartElement("privateDnsName"); sw.writeCharacters(addresses.get(0)); sw.writeEndElement(); sw.writeStartElement("dnsName"); sw.writeCharacters(addresses.get(0)); sw.writeEndElement(); sw.writeStartElement("instanceType"); sw.writeCharacters("m1.medium"); sw.writeEndElement(); sw.writeStartElement("placement"); { sw.writeStartElement("availabilityZone"); sw.writeCharacters("use-east-1e"); sw.writeEndElement(); sw.writeEmptyElement("groupName"); sw.writeStartElement("tenancy"); sw.writeCharacters("default"); sw.writeEndElement(); } sw.writeEndElement(); sw.writeStartElement("privateIpAddress"); sw.writeCharacters(addresses.get(0)); sw.writeEndElement(); sw.writeStartElement("ipAddress"); sw.writeCharacters(addresses.get(0)); sw.writeEndElement(); } sw.writeEndElement(); } sw.writeEndElement(); } sw.writeEndElement(); } } } sw.writeEndElement(); } sw.writeEndElement(); sw.writeEndDocument(); sw.flush(); final byte[] responseAsBytes = out.toString().getBytes(StandardCharsets.UTF_8); s.sendResponseHeaders(200, responseAsBytes.length); OutputStream responseBody = s.getResponseBody(); responseBody.write(responseAsBytes); responseBody.close(); } catch (XMLStreamException e) { Loggers.getLogger(Ec2DiscoveryClusterFormationTests.class).error("Failed serializing XML", e); throw new RuntimeException(e); } }); httpServer.start(); }
From source file:org.maodian.flyingcat.xmpp.extensions.xep0030.QueryItemCodec.java
@Override public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException { xmlsw.writeStartElement("", "query", ServiceDiscovery.ITEM); xmlsw.writeDefaultNamespace(ServiceDiscovery.ITEM); }
From source file:org.maodian.flyingcat.xmpp.extensions.xep0030.QueryInfoCodec.java
@Override public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException { xmlsw.writeStartElement("", "query", ServiceDiscovery.INFORMATION); xmlsw.writeDefaultNamespace(ServiceDiscovery.INFORMATION); QueryInfo qi = (QueryInfo) object;/* w ww. java 2s . c o m*/ List<Identity> identityList = qi.getIdentityList(); for (Identity identity : identityList) { xmlsw.writeEmptyElement("", "identity", ServiceDiscovery.INFORMATION); xmlsw.writeAttribute("category", identity.getCategory()); xmlsw.writeAttribute("type", identity.getType()); } List<Feature> featureList = qi.getFeatureList(); for (Feature feature : featureList) { xmlsw.writeEmptyElement(ServiceDiscovery.INFORMATION, "feature"); xmlsw.writeAttribute("var", feature.getVar()); } }
From source file:com.predic8.membrane.core.http.xml.Response.java
@Override public void write(XMLStreamWriter out) throws XMLStreamException { out.writeStartElement("http", ELEMENT_NAME, Constants.HTTP_NS); out.writeNamespace("http", Constants.HTTP_NS); out.writeStartElement("status"); out.writeAttribute("code", "" + statusCode); out.writeCharacters(statusMessage);//from w w w .ja v a 2 s. com out.writeEndElement(); writeIfNotNull(headers, out); if (body != null) { out.writeStartElement("body"); body.write(out); out.writeEndElement(); } out.writeEndElement(); }
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();/* w ww . j a va2s . c o m*/ }
From source file:org.maodian.flyingcat.xmpp.codec.RosterCodec.java
@Override public void encode(Object object, XMLStreamWriter xmlsw) throws XMLStreamException { Roster roster = (Roster) object;/*from www . jav a 2 s . c o m*/ xmlsw.writeStartElement("", "query", XmppNamespace.ROSTER); xmlsw.writeDefaultNamespace(XmppNamespace.ROSTER); writeAttributeIfNotBlank(xmlsw, "ver", roster.getVersion()); for (Contact c : roster) { xmlsw.writeEmptyElement("", "item", XmppNamespace.ROSTER); writeRequiredAttribute(xmlsw, "jid", c.getJabberId()); writeAttributeIfNotBlank(xmlsw, "name", c.getName()); writeAttributeIfNotBlank(xmlsw, "subscription", c.getSubscription()); writeAttributeIfNotBlank(xmlsw, "ask", c.getAsk()); } xmlsw.writeEndElement(); }