List of usage examples for javax.xml.stream XMLOutputFactory createXMLStreamWriter
public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;
From source file:Main.java
public static void main(String[] args) throws Exception { XMLOutputFactory xof = XMLOutputFactory.newFactory(); StringWriter sw = new StringWriter(); XMLStreamWriter xsw = xof.createXMLStreamWriter(sw); xsw.writeStartDocument();//ww w . ja v a2 s . c om xsw.writeStartElement("foo"); xsw.writeCharacters("<>\"&'"); xsw.writeEndDocument(); String xml = sw.toString(); System.out.println(xml); // READ THE XML XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); xsr.nextTag(); // Advance to "foo" element System.out.println(xsr.getElementText()); }
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();/*from ww w. ja v a 2s . c o m*/ writer.writeEndDocument(); writer.flush(); }
From source file:Main.java
private static XMLStreamWriter newWriter(ByteArrayOutputStream result) throws Exception { XMLOutputFactory output = XMLOutputFactory.newInstance(); XMLStreamWriter writer = output.createXMLStreamWriter(result); return writer; }
From source file:Main.java
public static XMLStreamWriter createStreamWriter() { if (stream != null) { System.out.println("ERROR: there is already an existing stream."); }//from ww w .java2 s . c o m stream = new ByteArrayOutputStream(); XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance(); try { XMLStreamWriter writer = xmlFactory.createXMLStreamWriter(stream); writer.writeStartDocument(); return writer; } catch (XMLStreamException e) { e.printStackTrace(); } return null; }
From source file:com.norconex.jefmon.model.ConfigurationDAO.java
public static void saveConfig(JEFMonConfig config) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Saving JEF config to: " + CONFIG_FILE); }//from w w w . j a v a 2s . co m if (!CONFIG_FILE.exists()) { File configDir = new File(FilenameUtils.getFullPath(CONFIG_FILE.getAbsolutePath())); if (!configDir.exists()) { LOG.debug("Creating JEF Monitor config directory for: " + CONFIG_FILE); configDir.mkdirs(); } } OutputStream out = new FileOutputStream(CONFIG_FILE); XMLOutputFactory factory = XMLOutputFactory.newInstance(); try { XMLStreamWriter xml = factory.createXMLStreamWriter(out); xml.writeStartDocument(); xml.writeStartElement("jefmon-config"); xml.writeStartElement("instance-name"); xml.writeCharacters(config.getInstanceName()); xml.writeEndElement(); xml.writeStartElement("default-refresh-interval"); xml.writeCharacters(Integer.toString(config.getDefaultRefreshInterval())); xml.writeEndElement(); saveRemoteUrls(xml, config.getRemoteInstanceUrls()); saveMonitoredPaths(xml, config.getMonitoredPaths()); saveJobActions(xml, config.getJobActions()); xml.writeEndElement(); xml.writeEndDocument(); xml.flush(); xml.close(); } catch (XMLStreamException e) { throw new IOException("Cannot save as XML.", e); } out.close(); }
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;/* ww w . java 2 s . c om*/ // <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 { 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:org.elasticsearch.discovery.ec2.Ec2DiscoveryClusterFormationTests.java
/** * Creates mock EC2 endpoint providing the list of started nodes to the DescribeInstances API call *///from ww w .j a v a 2 s . co m @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:eu.scape_project.planning.xml.ProjectExportAction.java
/** * Dumps binary data to provided file. It results in an XML file with a * single element: data.// w w w. j a v a2 s . c o m * * @param id * @param data * @param f * @param encoder * @throws IOException */ private static void writeBinaryData(int id, InputStream data, File f) throws IOException { XMLOutputFactory factory = XMLOutputFactory.newInstance(); try { XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter(f)); writer.writeStartDocument(PlanXMLConstants.ENCODING, "1.0"); writer.writeStartElement("data"); writer.writeAttribute("id", "" + id); Base64InputStream base64EncodingIn = new Base64InputStream(data, true, PlanXMLConstants.BASE64_LINE_LENGTH, PlanXMLConstants.BASE64_LINE_BREAK); OutputStream out = new WriterOutputStream(new XMLStreamContentWriter(writer), PlanXMLConstants.ENCODING); // read the binary data and encode it on the fly IOUtils.copy(base64EncodingIn, out); out.flush(); // all data is written - end writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); writer.close(); } catch (XMLStreamException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.esri.geoportal.commons.meta.js.XmlBuilder.java
/** * Initialize the builder.//from w w w .j a va 2 s . c o m * @throws Exception if initialization fails */ public void init() throws Exception { XMLOutputFactory factory = XMLOutputFactory.newInstance(); xml = new StringWriter(); writer = factory.createXMLStreamWriter(xml); }
From source file:com.norconex.collector.http.checksum.impl.DefaultHttpDocumentChecksummer.java
@Override public void saveToXML(Writer out) throws IOException { XMLOutputFactory factory = XMLOutputFactory.newInstance(); try {/* w w w . j av a 2 s. c o m*/ XMLStreamWriter writer = factory.createXMLStreamWriter(out); writer.writeStartElement("httpDocumentChecksummer"); writer.writeAttribute("class", getClass().getCanonicalName()); writer.writeStartElement("field"); writer.writeCharacters(field); writer.writeEndElement(); writer.writeEndElement(); writer.flush(); writer.close(); } catch (XMLStreamException e) { throw new IOException("Cannot save as XML.", e); } }