List of utility methods to do XML Format
String | formatXml(String xml) format Xml ByteArrayOutputStream output = new ByteArrayOutputStream(); try { readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output)); } catch (TransformerException e) { throw new RuntimeException("Failed to format XML", e); return output.toString(); |
String | formatXML(String xml) format XML try { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()); ... |
Transformer | formatXML(Transformer transformer) This method formats the XML file by omitting the XML Declaration and creating indentations transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); return transformer; |
String | formatXMLStr(String xml) format XML Str String output = null; try { Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ... |
String | prettifyString(String string) Returns a prettified version of the string, or the original string if the operation is not possible. String result = string; if (string.startsWith("<?xml")) { result = prettifyXML(string); return result; |
String | prettyFormat(String input) pretty Format return prettyFormat(input, 2, true);
|
String | prettyFormat(String input) Converts xml string to pretty format. Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { ... |
String | prettyFormat(String input) Format the provided XML input with a default indentation of 2. return prettyFormat(input, 2);
|
String | prettyFormat(String input, int indent) pretty Format try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent)); ... |
String | prettyFormat(String strInput, int nIndent) Pretty format a given XML document try { Source xmlInput = new StreamSource(new StringReader(strInput)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", nIndent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ... |