Java tutorial
//package com.java2s; //Software released under Common Public License (CPL) v1.0 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class Main { public static String tryFormattingString(String s) { try { ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { } @Override public void fatalError(SAXParseException exception) throws SAXException { } @Override public void error(SAXParseException exception) throws SAXException { } }); Document doc = builder.parse(in); TransformerFactory tf = TransformerFactory.newInstance(); // tf.setAttribute("indent-number", new Integer(2)); Transformer trans = tf.newTransformer(); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult result = new StreamResult(out); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.transform(source, result); return out.toString(); } catch (Exception e) { // Console.println("Could not validate the soapmessage, although I'll show it anyway.."); } return s; } }