public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException { XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = factory.createXMLStreamWriter(new FileOutputStream("output.xml")); writer.writeStartDocument(); writer.writeCharacters(" "); writer.writeComment("testing comment"); writer.writeCharacters(" "); writer.writeStartElement("catalogs"); writer.writeNamespace("myNS", "http://blog.csdn.net/Chinajash"); writer.writeAttribute("owner", "sina"); writer.writeCharacters(" "); writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog"); writer.writeAttribute("id", "007"); writer.writeCharacters("Apparel"); // 写入catalog元素的结束标签 writer.writeEndElement(); // 写入catalogs元素的结束标签 writer.writeEndElement(); // 结束 XML 文档 writer.writeEndDocument(); writer.close(); System.out.println("ok"); }
public static void writeDocument(Document doc, OutputStream outputStream, String namespaceURI) throws XMLStreamException, IOException { if(outputFactory == null) { outputFactory = XMLOutputFactory.newInstance(); } XMLStreamWriter xsw = null; try { if(doc instanceof TextualDocument) { xsw = outputFactory.createXMLStreamWriter(outputStream, ((TextualDocument)doc).getEncoding()); xsw.writeStartDocument(((TextualDocument)doc).getEncoding(), "1.0"); } else { xsw = outputFactory.createXMLStreamWriter(outputStream); xsw.writeStartDocument("1.0"); } newLine(xsw); writeDocument(doc, xsw, namespaceURI); } finally { if(xsw != null) { xsw.close(); } } }
public void writeTo(SOAPMessage saaj) throws SOAPException { try { // TODO what about in-scope namespaces // Not very efficient consider implementing a stream buffer // processor that produces a DOM node from the buffer. Transformer t = XmlUtil.newTransformer(); SOAPHeader header = saaj.getSOAPHeader(); if (header == null) header = saaj.getSOAPPart().getEnvelope().addHeader(); // TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be // uncommented and all lines below, except the catch block, can be removed. // t.transform(epr.asSource(localName), new DOMResult(header)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos); epr.writeTo(localName, w); w.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(true); Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement(); Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true); header.appendChild(eprNodeToAdd); } catch (Exception e) { throw new SOAPException(e); } }
/** * Initialize an instance of this XMLStreamWriter. Allocate new instances * for all the data structures. Set internal flags based on property values. */ private void init() { fReuse = false; fNamespaceDecls = new ArrayList(); fPrefixGen = new Random(); fAttributeCache = new ArrayList(); fInternalNamespaceContext = new NamespaceSupport(); fInternalNamespaceContext.reset(); fNamespaceContext = new NamespaceContextImpl(); fNamespaceContext.internalContext = fInternalNamespaceContext; // Set internal state based on property values Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES); fIsRepairingNamespace = ob.booleanValue(); ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS); setEscapeCharacters(ob.booleanValue()); }
/** * Reset this instance so that it can be re-used. Clears but does not * re-allocate internal data structures. * * @param resetProperties Indicates if properties should be read again */ void reset(boolean resetProperties) { if (!fReuse) { throw new java.lang.IllegalStateException( "close() Must be called before calling reset()"); } fReuse = false; fNamespaceDecls.clear(); fAttributeCache.clear(); // reset Element/NamespaceContext stacks fElementStack.clear(); fInternalNamespaceContext.reset(); fStartTagOpened = false; fNamespaceContext.userContext = null; if (resetProperties) { Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES); fIsRepairingNamespace = ob.booleanValue(); ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS); setEscapeCharacters(ob.booleanValue()); } }
public void writeTo(SOAPMessage saaj) throws SOAPException { try { // TODO what about in-scope namespaces // Not very efficient consider implementing a stream buffer // processor that produces a DOM node from the buffer. Transformer t = XmlUtil.newTransformer(); SOAPHeader header = saaj.getSOAPHeader(); if (header == null) header = saaj.getSOAPPart().getEnvelope().addHeader(); // TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be // uncommented and all lines below, except the catch block, can be removed. // t.transform(epr.asSource(localName), new DOMResult(header)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos); epr.writeTo(localName, w); w.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory fac = XmlUtil.newDocumentBuilderFactory(false); fac.setNamespaceAware(true); Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement(); Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true); header.appendChild(eprNodeToAdd); } catch (Exception e) { throw new SOAPException(e); } }
/** * Initialize an instance of this XMLStreamWriter. Allocate new instances * for all the data structures. Set internal flags based on property values. */ private void init() { fReuse = false; fNamespaceDecls = new ArrayList<>(); fPrefixGen = new Random(); fAttributeCache = new ArrayList<>(); fInternalNamespaceContext = new NamespaceSupport(); fInternalNamespaceContext.reset(); fNamespaceContext = new NamespaceContextImpl(); fNamespaceContext.internalContext = fInternalNamespaceContext; // Set internal state based on property values Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES); fIsRepairingNamespace = ob; ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS); setEscapeCharacters(ob); }
/** * Reset this instance so that it can be re-used. Clears but does not * re-allocate internal data structures. * * @param resetProperties Indicates if properties should be read again */ void reset(boolean resetProperties) { if (!fReuse) { throw new java.lang.IllegalStateException( "close() Must be called before calling reset()"); } fReuse = false; fNamespaceDecls.clear(); fAttributeCache.clear(); // reset Element/NamespaceContext stacks fElementStack.clear(); fInternalNamespaceContext.reset(); fStartTagOpened = false; fNamespaceContext.userContext = null; if (resetProperties) { Boolean ob = (Boolean) fPropertyManager.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES); fIsRepairingNamespace = ob; ob = (Boolean) fPropertyManager.getProperty(Constants.ESCAPE_CHARACTERS); setEscapeCharacters(ob); } }
@Test public void testStreamReader() { XMLInputFactory ifac = XMLInputFactory.newInstance(); XMLOutputFactory ofac = XMLOutputFactory.newInstance(); try { ifac.setProperty(ifac.IS_REPLACING_ENTITY_REFERENCES, new Boolean(false)); XMLStreamReader re = ifac.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(), this.getClass().getResourceAsStream(INPUT_FILE)); while (re.hasNext()) { int event = re.next(); if (event == XMLStreamConstants.START_ELEMENT && re.getLocalName().equals("bookurn")) { Assert.assertTrue(re.getAttributeCount() == 0, "No attributes are expected for <bookurn> "); Assert.assertTrue(re.getNamespaceCount() == 2, "Two namespaces are expected for <bookurn> "); } } } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
/** * Test XMLStreamWriter parsing a file with an external entity reference. */ @Test public void testXMLStreamWriter() { try { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); String file = getClass().getResource("XMLEventWriterTest.xml").getPath(); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file))); // adds the event to the consumer. eventWriter.add(eventReader); eventWriter.flush(); eventWriter.close(); // expected success } catch (Exception exception) { exception.printStackTrace(); Assert.fail(exception.toString()); } }
@Test public void test() { try { XMLOutputFactory xof = XMLOutputFactory.newInstance(); StreamResult sr = new StreamResult(); XMLStreamWriter xsw = xof.createXMLStreamWriter(sr); NamespaceContext nc = xsw.getNamespaceContext(); System.out.println(nc.getPrefix(XMLConstants.XML_NS_URI)); System.out.println(" expected result: " + XMLConstants.XML_NS_PREFIX); System.out.println(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)); System.out.println(" expected result: " + XMLConstants.XMLNS_ATTRIBUTE); Assert.assertTrue(nc.getPrefix(XMLConstants.XML_NS_URI) == XMLConstants.XML_NS_PREFIX); Assert.assertTrue(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) == XMLConstants.XMLNS_ATTRIBUTE); } catch (Throwable ex) { Assert.fail(ex.toString()); } }
/** * @bug 8139584 * Verifies that the resulting XML contains the standalone setting. */ @Test public void testCreateStartDocument() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); XMLOutputFactory out = XMLOutputFactory.newInstance(); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEventWriter eventWriter = out.createXMLEventWriter(stringWriter); XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true); eventWriter.add(event); eventWriter.flush(); Assert.assertTrue(stringWriter.toString().contains("encoding=\"iso-8859-15\"")); Assert.assertTrue(stringWriter.toString().contains("version=\"1.0\"")); Assert.assertTrue(stringWriter.toString().contains("standalone=\"yes\"")); }
/** * @bug 8139584 * Verifies that the resulting XML contains the standalone setting. */ @Test public void testCreateStartDocument_DOMWriter() throws ParserConfigurationException, XMLStreamException { XMLOutputFactory xof = XMLOutputFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true); eventWriter.add(event); eventWriter.flush(); Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15"); Assert.assertEquals(doc.getXmlVersion(), "1.0"); Assert.assertTrue(doc.getXmlStandalone()); }
/** * Test of main method, of class TestXMLStreamWriter. */ @Test public void testWriteComment() { try { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html href=\"http://java.sun.com\"><!--This is comment-->java.sun.com</a:html>"; XMLOutputFactory f = XMLOutputFactory.newInstance(); // f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, // Boolean.TRUE); StringWriter sw = new StringWriter(); XMLStreamWriter writer = f.createXMLStreamWriter(sw); writer.writeStartDocument("UTF-8", "1.0"); writer.writeStartElement("a", "html", "http://www.w3.org/TR/REC-html40"); writer.writeAttribute("href", "http://java.sun.com"); writer.writeComment("This is comment"); writer.writeCharacters("java.sun.com"); writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); sw.flush(); StringBuffer sb = sw.getBuffer(); System.out.println("sb:" + sb.toString()); Assert.assertTrue(sb.toString().equals(xml)); } catch (Exception ex) { Assert.fail("Exception: " + ex.getMessage()); } }
@Test public void testUnboundPrefix() throws Exception { try { XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter w = xof.createXMLStreamWriter(System.out); // here I'm trying to write // <bar xmlns="foo" /> w.writeStartDocument(); w.writeStartElement("foo", "bar"); w.writeDefaultNamespace("foo"); w.writeCharacters("---"); w.writeEndElement(); w.writeEndDocument(); w.close(); // Unexpected success String FAIL_MSG = "Unexpected success. Expected: " + "XMLStreamException - " + "if the namespace URI has not been bound to a prefix " + "and javax.xml.stream.isPrefixDefaulting has not been " + "set to true"; System.err.println(FAIL_MSG); Assert.fail(FAIL_MSG); } catch (XMLStreamException xmlStreamException) { // Expected Exception System.out.println("Expected XMLStreamException: " + xmlStreamException.toString()); } }
@Test public void testBoundPrefix() throws Exception { try { XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter w = xof.createXMLStreamWriter(System.out); // here I'm trying to write // <bar xmlns="foo" /> w.writeStartDocument(); w.writeStartElement("foo", "bar", "http://namespace"); w.writeCharacters("---"); w.writeEndElement(); w.writeEndDocument(); w.close(); // Expected success System.out.println("Expected success."); } catch (Exception exception) { // Unexpected Exception String FAIL_MSG = "Unexpected Exception: " + exception.toString(); System.err.println(FAIL_MSG); Assert.fail(FAIL_MSG); } }
@BeforeMethod public void setUp() { // want a Factory that repairs Namespaces xmlOutputFactory = XMLOutputFactory.newInstance(); xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); // new OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // new Writer try { xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream, "utf-8"); } catch (XMLStreamException xmlStreamException) { Assert.fail(xmlStreamException.toString()); } }
@Test public void testSAXResult() { DefaultHandler handler = new DefaultHandler(); final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>"; try { SAXResult saxResult = new SAXResult(handler); // saxResult.setSystemId("jaxp-ri/unit-test/javax/xml/stream/XMLOutputFactoryTest/cr6846132.xml"); XMLOutputFactory ofac = XMLOutputFactory.newInstance(); XMLStreamWriter writer = ofac.createXMLStreamWriter(saxResult); writer.writeStartDocument("1.0"); writer.writeStartElement("root"); writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); writer.close(); } catch (Exception e) { if (e instanceof UnsupportedOperationException) { // expected } else { e.printStackTrace(); Assert.fail(e.toString()); } } }
@Test public void testSAXResult1() { DefaultHandler handler = new DefaultHandler(); try { SAXResult saxResult = new SAXResult(handler); XMLOutputFactory ofac = XMLOutputFactory.newInstance(); XMLEventWriter writer = ofac.createXMLEventWriter(saxResult); } catch (Exception e) { if (e instanceof UnsupportedOperationException) { // expected } else { e.printStackTrace(); Assert.fail(e.toString()); } } }
@Test public void testStreamResult() { final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); StreamResult sr = new StreamResult(buffer); XMLStreamWriter writer = ofac.createXMLStreamWriter(sr); writer.writeStartDocument("1.0"); writer.writeStartElement("root"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }
@Test public void testStreamWriterWithStAXResultNStreamWriter() { final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>"; try { XMLOutputFactory ofac = XMLOutputFactory.newInstance(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLStreamWriter writer = ofac.createXMLStreamWriter(buffer); StAXResult res = new StAXResult(writer); writer = ofac.createXMLStreamWriter(res); writer.writeStartDocument("1.0"); writer.writeStartElement("root"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.toString()); } }
public static void writeXml(Path configDir, Path xmlFile) throws IOException, XMLStreamException { XMLOutputFactory output = XMLOutputFactory.newInstance(); try (Writer writer = Files.newBufferedWriter(xmlFile, StandardCharsets.UTF_8)) { XMLStreamWriter xmlWriter = output.createXMLStreamWriter(writer); try { xmlWriter.writeStartDocument(StandardCharsets.UTF_8.toString(), "1.0"); xmlWriter.writeStartElement("config"); try (DirectoryStream<Path> ds = Files.newDirectoryStream(configDir, entry -> Files.isRegularFile(entry) && entry.getFileName().toString().endsWith(".properties"))) { for (Path file : ds) { String fileName = file.getFileName().toString(); String fileNameWithoutExtension = fileName.substring(0, fileName.length() - 11); xmlWriter.writeStartElement(fileNameWithoutExtension); Properties properties = new Properties(); try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { properties.load(reader); } for (String name : properties.stringPropertyNames()) { String value = properties.getProperty(name); xmlWriter.writeStartElement(name); xmlWriter.writeCharacters(value); xmlWriter.writeEndElement(); } xmlWriter.writeEndElement(); } } xmlWriter.writeEndElement(); xmlWriter.writeEndDocument(); } finally { xmlWriter.close(); } } }
public static void exportStore(final ExportConfiguration config) throws Exception { if (!config.isOverwrite() && config.getTarget().exists()) { throw new IllegalStateException("File: " + config.getTarget() + " already exists"); } long start = System.currentTimeMillis(); try(OutputStream fos = new BufferedOutputStream(config.isCompress() ? new GZIPOutputStream( new FileOutputStream(config.getTarget())) : new FileOutputStream(config.getTarget()))) { final XMLStreamWriter xmlWriter = XMLOutputFactory.newFactory().createXMLStreamWriter(fos); final ArtemisJournalMarshaller xmlMarshaller = new ArtemisJournalMarshaller(xmlWriter); xmlMarshaller.appendJournalOpen(); if (config.isMultiKaha()) { appendMultiKahaDbStore(xmlMarshaller, getMultiKahaDbAdapter(config.getSource()), config.getQueuePattern(), config.getTopicPattern()); } else { appendKahaDbStore(xmlMarshaller, getKahaDbAdapter(config.getSource()), config.getQueuePattern(), config.getTopicPattern()); } xmlMarshaller.appendJournalClose(true); } long end = System.currentTimeMillis(); LOG.info("Total export time: " + (end - start) + " ms"); }
/** * Test stream marshal by appending one message at a time * * @throws Exception */ @Test public void testStreamMarshal() throws Exception { File file = tempFolder.newFile(); try(FileOutputStream fos = new FileOutputStream(file)) { XMLStreamWriter xmlWriter = XMLOutputFactory.newFactory().createXMLStreamWriter(fos); ArtemisJournalMarshaller xmlMarshaller = new ArtemisJournalMarshaller(xmlWriter); xmlMarshaller.appendJournalOpen(); xmlMarshaller.appendBindingsElement(); xmlMarshaller.appendBinding(new AddressBindingType()); xmlMarshaller.appendEndElement(); xmlMarshaller.appendMessagesElement(); //Marshal messages one at a time for (int i = 0; i < 3; i++) { MessageType message = new MessageType(); message.setId((long) i); message.setTimestamp(System.currentTimeMillis()); xmlMarshaller.appendMessage(message); } xmlMarshaller.appendEndElement(); xmlMarshaller.appendJournalClose(true); } //This can be read as a stream as well but for the purpose of this test //just read the whole thing in at once validate(file); }
public static void serializeCompany(File output, Company c) throws JAXBException, FileNotFoundException, XMLStreamException { initializeJaxbContext(); OutputStream os = new FileOutputStream(output); Marshaller marshaller = jaxbContext.createMarshaller(); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os); marshaller.marshal(c, writer); // TODO: need a stream writer that does indentation }
public static void writeCompany(File output, Company c) throws JAXBException, FileNotFoundException, XMLStreamException { initializeJaxbContext(); OutputStream os = new FileOutputStream(output); Marshaller marshaller = jaxbContext.createMarshaller(); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os); marshaller.marshal(c, writer); // TODO: need a stream writer that does indentation }
public void createInitXML(String xml_path,String subnet,String count) throws SAXException, ParserConfigurationException, IOException, XMLStreamException{ XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; xtw = xof.createXMLStreamWriter(new FileWriter("init.xml")); xtw.writeStartDocument("utf-8","1.0"); xtw.writeCharacters("\n"); xtw.writeStartElement("SETTINGS"); xtw.writeCharacters("\n"); xtw.writeStartElement("XML_PATH"); xtw.writeCharacters("\n"); xtw.writeCharacters(xml_path); xtw.writeCharacters("\n"); xtw.writeEndElement(); xtw.writeCharacters("\n"); xtw.writeStartElement("SUBNET"); xtw.writeCharacters("\n"); xtw.writeCharacters(subnet); xtw.writeCharacters("\n"); xtw.writeEndElement(); xtw.writeCharacters("\n"); xtw.writeStartElement("count"); xtw.writeCharacters("\n"); xtw.writeCharacters(count); xtw.writeCharacters("\n"); xtw.writeEndElement(); xtw.writeCharacters("\n"); xtw.writeEndElement(); xtw.writeCharacters("\n"); xtw.writeEndDocument(); xtw.flush(); xtw.close(); }
@DataProvider(name = "jaxpFactories") public Object[][] jaxpFactories() throws Exception { return new Object[][] { { DocumentBuilderFactory.newInstance(), (Produce)factory -> ((DocumentBuilderFactory)factory).newDocumentBuilder() }, { SAXParserFactory.newInstance(), (Produce)factory -> ((SAXParserFactory)factory).newSAXParser() }, { SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI), (Produce)factory -> ((SchemaFactory)factory).newSchema() }, { TransformerFactory.newInstance(), (Produce)factory -> ((TransformerFactory)factory).newTransformer() }, { XMLEventFactory.newInstance(), (Produce)factory -> ((XMLEventFactory)factory).createStartDocument() }, { XMLInputFactory.newInstance(), (Produce)factory -> ((XMLInputFactory)factory).createXMLEventReader(new StringReader("")) }, { XMLOutputFactory.newInstance(), (Produce)factory -> ((XMLOutputFactory)factory).createXMLEventWriter(new StringWriter()) }, { XPathFactory.newInstance(), (Produce)factory -> ((XPathFactory)factory).newXPath() }, { DatatypeFactory.newInstance(), (Produce)factory -> ((DatatypeFactory)factory).newXMLGregorianCalendar() } }; }
public XMLStreamWriter getXMLStreamWriter(AutoFlushPolicy autoFlush) { try { return XMLStreamWriterProxy.proxyFor(XMLOutputFactory.newFactory().createXMLStreamWriter(getOutputStream()), autoFlush == AutoFlushPolicy.AUTO_FLUSH); } catch (XMLStreamException ex) { throw new IllegalStateException(ex); } }
@BeforeClass public void setup() throws Exception { ifac = XMLInputFactory.newInstance(); ofac = XMLOutputFactory.newInstance(); tfac = TransformerFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); db = dbf.newDocumentBuilder(); xml = Files.readAllBytes(Paths.get(XML_DIR + "cities.xml")); template = Files.readAllBytes(Paths.get(XML_DIR + "cities.xsl")); xmlDoc = db.parse(xmlInputStream()); }
public static XMLStreamWriter newXMLStreamWriter(OutputStream out) { try { return XMLOutputFactory.newFactory().createXMLStreamWriter(out); } catch (XMLStreamException ex) { throw new IllegalStateException(ex); } }
public static XMLEventWriter newXMLEventWriter(OutputStream out) { try { return XMLOutputFactory.newFactory().createXMLEventWriter(out); } catch (XMLStreamException ex) { throw new IllegalStateException(ex); } }
@Override public void writeTo(HashMap hashMap, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException { try { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(outputStream); xmlWriter.writeStartElement("entity"); writeValue(hashMap, xmlWriter); xmlWriter.writeEndElement(); xmlWriter.flush(); xmlWriter.close(); } catch (XMLStreamException e) { throw new IOException(e); } }
@Override public void writeTo(ArrayList arrayList, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException { try { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(outputStream); writeValue(arrayList, xmlWriter); xmlWriter.flush(); xmlWriter.close(); } catch (XMLStreamException e) { throw new IOException(e); } }
/** * Returns a string containing the specified document in GATE XML * format. * * @param doc the document */ public static String toXml(Document doc) { try { if(outputFactory == null) { outputFactory = XMLOutputFactory.newInstance(); } StringWriter sw = new StringWriter(doc.getContent().size().intValue() * DocumentXmlUtils.DOC_SIZE_MULTIPLICATION_FACTOR); XMLStreamWriter xsw = outputFactory.createXMLStreamWriter(sw); // start the document if(doc instanceof TextualDocument) { xsw.writeStartDocument(((TextualDocument)doc).getEncoding(), "1.0"); } else { xsw.writeStartDocument("1.0"); } newLine(xsw); writeDocument(doc, xsw, ""); xsw.close(); return sw.toString(); } catch(XMLStreamException xse) { throw new GateRuntimeException("Error converting document to XML", xse); } }
public XMLStreamer(Writer writer) { this.output = writer; try { xml = XMLOutputFactory.newInstance().createXMLStreamWriter(output); } catch( Exception ex ) { throw new RuntimeException(ex); } }