List of utility methods to do XML Document Save to File
void | saveReportToFile(Node node, String documentFileName) save Report To File File output = new File(documentFileName); StreamResult resultFile = new StreamResult(output); DOMSource source = new DOMSource(node); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); ... |
void | saveTemporaryDocument(Document document, String folderName) save Temporary Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); String str = writer.writeToString(document); str = str.replace("UTF-16", "UTF-8"); File outputFolder = new File(folderName); outputFolder.mkdirs(); String fileName = outputFolder.getAbsolutePath() + "/" + "file_" + String.valueOf(index) + ".xml"; ... |
void | saveToDisk(Document doc, OutputStream out) save To Disk Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(out)); |
void | saveToFile(Document doc, File f, boolean indent) save To File Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8")); try { int n = indent ? 4 : 0; writeByDom(doc, writer, n); } finally { writer.close(); |
File | saveToFile(String filename, Document document) save To File Source source = new DOMSource(document); File file = new File(filename); Result result = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); return file; ... |
void | saveToXml(Document doc, OutputStream os) save To Xml TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(os); DOMSource source = new DOMSource(doc); transformer.transform(source, result); |
String | saveToXmlStr(Document doc) serializing a XML DOM Document into XML String
TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = sw.toString(); ... |
void | saveXML(Document doc, ByteArrayOutputStream outputStreamXML) Serializa un XML a ByteArrayOutputStream System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementation domImpl = registry.getDOMImplementation("LS 3.0"); DOMImplementationLS implLS = (DOMImplementationLS) domImpl; LSSerializer dom3Writer = implLS.createLSSerializer(); LSOutput output = implLS.createLSOutput(); output.setByteStream(outputStreamXML); output.setEncoding("UTF-8"); ... |
boolean | saveXML(Document doc, File outfile, boolean indent) save XML try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")); if (indent) { XPathFactory xpathFactory = XPathFactory.newInstance(); XPathExpression xpathExp = xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']"); NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET); ... |
void | saveXml(Document doc, String filename) save Xml try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); ... |