Java XML String Transform indentXML(String fileContent)

Here you can find the source of indentXML(String fileContent)

Description

Indents the given XML file content.

License

Open Source License

Parameter

Parameter Description
fileContent the XML file content.

Return

the indented XML file content

Declaration

private static String indentXML(String fileContent) 

Method Source Code

//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;
    }
}

Related

  1. getStringFromStreamSource(StreamSource src)
  2. getTemplates(String name)
  3. getValidator(String path)
  4. getXmlStringFromSource(Source payload)
  5. identity(String xml)
  6. loadSourceFromURL(String systemID)
  7. mergeXml(String... xmlSources)
  8. normXML(String s)
  9. parseEventsToXML(String module, Hashtable keyvalues)