FileOutputStream fos = null; try { fos = new FileOutputStream("test.xml"); XMLOutputFactory xmlOutFact = XMLOutputFactory.newInstance(); XMLStreamWriter writer = xmlOutFact.createXMLStreamWriter(fos); writer.writeStartDocument(); writer.writeStartElement("test"); // write stuff writer.writeEndElement(); writer.flush(); } catch(IOException exc) { } catch(XMLStreamException exc) { } finally { }
public void close() { try { stream.writeEndDocument(); stream.flush(); stream.close(); } catch (XMLStreamException e) { throw rethrow(e); } }
@Override public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { if(!fragment) { out.writeEndDocument(); out.flush(); } super.endDocument(fragment); }
private void writeFile(TimerImpl timer) { final File file = fileName(timer.getTimedObjectId(), timer.getId()); //if the timer is expired or cancelled delete the file if (timer.getState() == TimerState.CANCELED || timer.getState() == TimerState.EXPIRED) { if (file.exists()) { file.delete(); } return; } try { FileOutputStream out = new FileOutputStream(file); try { XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out); XMLMapper mapper = createMapper(timer.getTimerService()); mapper.deparseDocument(new EjbTimerXmlPersister(factory, configuration), Collections.singletonList(timer), writer); writer.flush(); writer.close(); } finally { safeClose(out); } } catch (Exception e) { throw new RuntimeException(e); } }
/** * Write the /crs:defintions and the end document and flush the writer. * * @param xmlWriter * @throws XMLStreamException */ protected void endDocument( XMLStreamWriter xmlWriter ) throws XMLStreamException { xmlWriter.writeEndElement(); // </crs:definitions> xmlWriter.writeEndDocument(); xmlWriter.flush(); }
public ContentType encode(Packet packet, OutputStream out) { Message message = packet.getMessage(); if (message != null && message.hasPayload()) { final XMLStreamWriter writer = getXMLStreamWriter(out); try { writer.writeStartDocument(); packet.getMessage().writePayloadTo(writer); writer.writeEndDocument(); writer.flush(); } catch (XMLStreamException e) { throw new WebServiceException(e); } } return _contentType; }
XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out); xmlStreamWriter.writeStartDocument(); xmlStreamWriter.writeStartElement("YourRootElement"); xmlStreamWriter.writeNamespace("xsi", "http://www.w3.org/2000/10/XMLSchema-instance"); xmlStreamWriter.writeAttribute("http://www.w3.org/2000/10/XMLSchema-instance", "noNamespaceSchemaLocation", "path_to_your.xsd"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.flush();
public String getStringXML() { try { writer.writeEndElement(); writer.flush(); writer.close(); return stream.getBuffer().toString(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }
private static void dtoddi(DatasetDTO datasetDto, OutputStream outputStream) throws XMLStreamException { XMLStreamWriter xmlw = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream); xmlw.writeStartElement("codeBook"); xmlw.writeDefaultNamespace("ddi:codebook:2_5"); xmlw.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); xmlw.writeAttribute("xsi:schemaLocation", "ddi:codebook:2_5 http://www.ddialliance.org/Specification/DDI-Codebook/2.5/XMLSchema/codebook.xsd"); writeAttribute(xmlw, "version", "2.5"); createStdyDscr(xmlw, datasetDto); createOtherMats(xmlw, datasetDto.getDatasetVersion().getFiles()); xmlw.writeEndElement(); // codeBook xmlw.flush(); }
protected void writePreferences(PreferencesImpl prefs, XMLStreamWriter writer) throws XMLStreamException { final Iterator<?> i = prefs.getProperties().entrySet().iterator(); while (i.hasNext()) { final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next(); writer.writeStartElement(entry.getKey().toString()); writer.writeCharacters(EscapeChars.forXML(entry.getValue().toString())); writer.writeEndElement(); } writer.flush(); } }
public static void serialize(Element element, Writer writer) throws IOException, XMLStreamException { XMLStreamWriter xml = outputFactory.createXMLStreamWriter(writer); serialize(element, xml); xml.writeEndDocument(); xml.flush(); }
@Override public void exportDataset(DatasetVersion version, JsonObject json, OutputStream outputStream) throws ExportException { try { XMLStreamWriter xmlw = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream); xmlw.writeStartDocument(); xmlw.flush(); DdiExportUtil.datasetJson2ddi(json, version, outputStream); } catch (XMLStreamException xse) { throw new ExportException ("Caught XMLStreamException performing DDI export"); } }
/** {@inheritDoc} */ @Override public ByteBuffer toByteBuffer(Object obj) { if (obj == null) { return null; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { XMLStreamWriter writer = createStreamWriter(buffer); marshaller.get().marshal(obj, writer); writer.flush(); writer.close(); } catch (JAXBException e) { throw new HectorSerializationException("Object to serialize " + obj + " does not seem compatible with the configured JaxbContext;" + " note this Serializer works only with JAXBable objects.", e); } catch (XMLStreamException e) { throw new HectorSerializationException( "Exception occurred writing XML stream.", e); } return ByteBuffer.wrap(buffer.toByteArray()); }
public static String policyComponentToString(PolicyComponent policyComponent) throws XMLStreamException, FactoryConfigurationError { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter writer = XMLOutputFactory.newInstance() .createXMLStreamWriter(baos); policyComponent.serialize(writer); writer.flush(); return baos.toString(); }
@Override public String getFingerprint() throws AuthorizationAccessException { final List<AccessPolicy> policies = new ArrayList<>(getAccessPolicies()); Collections.sort(policies, Comparator.comparing(AccessPolicy::getIdentifier)); XMLStreamWriter writer = null; final StringWriter out = new StringWriter(); try { writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); writer.writeStartDocument(); writer.writeStartElement("accessPolicies"); for (AccessPolicy policy : policies) { writePolicy(writer, policy); } writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); } catch (XMLStreamException e) { throw new AuthorizationAccessException("Unable to generate fingerprint", e); } finally { if (writer != null) { try { writer.close(); } catch (XMLStreamException e) { // nothing to do here } } } return out.toString(); }
public Xpp3Dom endXML() { try { writer.writeEndDocument(); writer.flush(); writer.close(); Xpp3Dom dom = null; try { dom = Xpp3DomBuilder.build(new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray()))); } catch (XmlPullParserException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } return dom; } catch (XMLStreamException e) { throw new RuntimeException(e); } }
xmlWriter = outputFactory.createXMLStreamWriter(baos, "UTF-8"); xmlWriter = outputFactory.createXMLStreamWriter(baos, "UTF-8"); xmlWriter.flush(); xmlWriter.close();
private void closeDocument() throws XMLStreamException { if (!headerWritten) writeHeader(null); writer.writeEndElement(); writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); }
public ContentType encode(Packet packet, OutputStream out) { Message message = packet.getMessage(); if (message != null && message.hasPayload()) { final XMLStreamWriter writer = getXMLStreamWriter(out); try { writer.writeStartDocument(); packet.getMessage().writePayloadTo(writer); writer.writeEndDocument(); writer.flush(); } catch (XMLStreamException e) { throw new WebServiceException(e); } } return _contentType; }
@Override public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { if(!fragment) { out.writeEndDocument(); out.flush(); } super.endDocument(fragment); }