1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   | File file= new File(path + File.separator + xmlFile);
 
 
        String xslt = 
            "<?xml version=\"1.0\"?>"+
            "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:xalan=\"http://xml.apache.org/xslt\" version=\"1.0\">"+
            "  <xsl:output method=\"xml\" indent=\"yes\" xalan:indent-amount=\"3\" />"+
            "  <!-- copy out the xml -->"+
            "  <xsl:template match=\"* | @*\">"+
            "    <xsl:copy>"+
            "      <xsl:copy-of select=\"@*\" />"+
            "      <xsl:apply-templates />"+
            "    </xsl:copy>"+
            "  </xsl:template>"+
            "</xsl:stylesheet>";
 
        Source xmlSource = new StreamSource(file);
        Source xslSource = new StreamSource(xslt);
 
        StringWriter writer = new StringWriter();
        Result destResult = new StreamResult(writer);
 
        TransformerFactory transFact = TransformerFactory.newInstance();
 
        try{ 
            Transformer transFinal = transFact.newTransformer(xslSource);
            transFinal.transform(xmlSource, destResult);
            FileWriter fwriter = new FileWriter(file);
            fwriter.write(writer.toString());
            fwriter.close();
        }        
        catch(Exception e){
            throw new ConfigException("Unable to indent XML file! "+e.getMessage());
        } | 
Partager