/** * Writes the specified DOM to the given output stream. * * @param os the {@link OutputStream} to which XML should be written * @param doc the {@link Document} representing the XML to write * @throws TransformerException if there is an error during writing */ public static void writeXML(OutputStream os, Document doc) throws TransformerException { writeXML(os, doc, true); }
/** Writes the specified DOM to the given output stream. */ public static void writeXML(OutputStream os, Document doc) throws TransformerException { writeXML(os, doc, true); }
/** Writes the specified DOM to the given output stream. */ public static void writeXML(OutputStream os, Document doc, boolean includeXMLDeclaration) throws TransformerException { writeXML(new StreamResult(os), doc, includeXMLDeclaration); }
/** * Writes the specified DOM to the given output stream. * * @param os the {@link OutputStream} to which XML should be written * @param doc the {@link Document} representing the XML to write * @param includeXMLDeclaration false if the XML declaration should * be omitted, see {@link OutputKeys#OMIT_XML_DECLARATION} * @throws TransformerException if there is an error during writing */ public static void writeXML(OutputStream os, Document doc, boolean includeXMLDeclaration) throws TransformerException { writeXML(new StreamResult(os), doc, includeXMLDeclaration); }
/** Converts the given DOM back to a string. */ public static String getXML(Document doc) throws TransformerConfigurationException, TransformerException { StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); writeXML(result, doc, true); return stringWriter.getBuffer().toString(); }
/** * Converts the given DOM back to a string. * * @param doc the {@link Document} representing the XML * @return a string representation of the XML * @throws TransformerConfigurationException if there is an error * initializing the conversion * @throws TransformerException if there is an error during writing */ public static String getXML(Document doc) throws TransformerConfigurationException, TransformerException { StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); writeXML(result, doc, true); return stringWriter.getBuffer().toString(); }
/** * Dumps the given OME-XML DOM tree to a string. * @param schemaLocation if null, no xmlns attribute will be added. * @return OME-XML as a string. */ public static String dumpXML(String schemaLocation, Document doc, Element r, boolean includeXMLDeclaration) { try { ByteArrayOutputStream os = new ByteArrayOutputStream(); if (schemaLocation != null) { r.setAttribute("xmlns:xsi", XSI_NS); r.setAttribute("xsi:schemaLocation", schemaLocation); } doc.appendChild(r); writeXML(os, doc, includeXMLDeclaration); return os.toString(Constants.ENCODING); } catch (TransformerException exc) { LOGGER.warn("Failed to create XML", exc); throw new RuntimeException(exc); } catch (UnsupportedEncodingException exc) { LOGGER.warn("Failed to create XML", exc); throw new RuntimeException(exc); } }
/** * Dumps the given OME-XML DOM tree to a string. * @param schemaLocation if null, no xmlns attribute will be added. * @param doc the {@link Document} representing the XML to be written * @param r the {@link Element} to use for adding the schema location * @param includeXMLDeclaration false if the XML declaration should * be omitted, see {@link OutputKeys#OMIT_XML_DECLARATION} * @return OME-XML as a string. */ public static String dumpXML(String schemaLocation, Document doc, Element r, boolean includeXMLDeclaration) { try { ByteArrayOutputStream os = new ByteArrayOutputStream(); if (schemaLocation != null) { r.setAttribute("xmlns:xsi", XSI_NS); r.setAttribute("xsi:schemaLocation", schemaLocation); } doc.appendChild(r); writeXML(os, doc, includeXMLDeclaration); return os.toString(Constants.ENCODING); } catch (TransformerException exc) { LOGGER.warn("Failed to create XML", exc); throw new RuntimeException(exc); } catch (UnsupportedEncodingException exc) { LOGGER.warn("Failed to create XML", exc); throw new RuntimeException(exc); } }
FileOutputStream fos = new FileOutputStream( file); XMLTools.writeXML(fos, document); fos.close(); retrieve = null;