Java XML Format formatXML(byte[] xmlData, int indent)

Here you can find the source of formatXML(byte[] xmlData, int indent)

Description

format XML

License

Open Source License

Declaration

public static String formatXML(byte[] xmlData, int indent) throws TransformerException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    public static String formatXML(byte[] xmlData, int indent) throws TransformerException {

        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();
    }//  www  . ja v  a 2 s.  c  o  m
}

Related

  1. format(Node node)
  2. format(Node node, String indent)
  3. format(String unformattedXml)
  4. formatAttributes(Node node)
  5. formattedPrint(Node xml, OutputStream out)
  6. formatXML(String unformatted)
  7. formatXml(String xml)
  8. formatXML(String xml)
  9. formatXml(String xml)