The list of methods to do XML Format are organized into topic(s).
String
format(final String xml) Parse string as XML document and return string with reformatted document.
return toString(getXMLDocument(xml));
String
format(Node node) format
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(sw));
...
String
format(Node node, String indent) Format generated xml doc by indentation
StringBuilder formatted = new StringBuilder();
if (node.getNodeType() == Node.ELEMENT_NODE) {
StringBuilder attributes = new StringBuilder();
for (int k = 0; k < node.getAttributes().getLength(); k++) {
attributes.append(" ");
attributes.append(node.getAttributes().item(k).getNodeName());
attributes.append("=\"");
attributes.append(node.getAttributes().item(k).getNodeValue());
...
String
format(String unformattedXml) format
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Transformer transformer;
transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(out);
StreamSource source = new StreamSource(new ByteArrayInputStream(unformattedXml.getBytes()));
transformer.transform(source, result);
...
String
formatAttributes(Node node) Returns formatted attributes of the node.
StringBuilder sb = new StringBuilder();
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node attr = attrs.item(i);
sb.append(' ').append(attr.getNodeName()).append("= '").append(attr.getNodeValue()).append("'");
return sb.toString();
void
formattedPrint(Node xml, OutputStream out) formatted Print
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer tf = tFactory.newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
StreamResult result = new StreamResult(new OutputStreamWriter(out, "UTF-8"));
...
String
formatXML(byte[] xmlData, int indent) format XML
Source xmlInput = new StreamSource(new ByteArrayInputStream(xmlData));
StreamResult xmlOutput = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", indent);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
...
String
formatXML(String unformatted) format XML
if (unformatted == null)
return null;
String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><");
Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...
String
formatXml(String xml) Takes given XML and indents everything with 2 spaces.
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 2);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...