Here you can find the source of indentXML(String fileContent)
Parameter | Description |
---|---|
fileContent | the XML file content. |
private static String indentXML(String fileContent)
//package com.java2s; /******************************************************************************* * Copyright (c) 2016 Obeo. //from www. jav a2 s .co m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Obeo - initial API and implementation * *******************************************************************************/ import java.io.StringReader; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class Main { /** * Indents the given XML file content. * * @param fileContent * the XML file content. * @return the indented XML file content */ private static String indentXML(String fileContent) { final String res; StreamResult xmlOutput = null; try { // Configure transformer Transformer transformer; Source xmlInput = new StreamSource( new StringReader(fileContent)); xmlOutput = new StreamResult(new StringWriter()); transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { e.printStackTrace(); } } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } if (xmlOutput != null) { res = xmlOutput.getWriter().toString(); } else { res = fileContent; } return res; } }