XML formatting in java - Indentation management

I have an XML file that I open and edit several attributes in node and then save it, but for some reason, the saved XML does not indent properly, as before.

Here is my code through which I save the XML file:

    TransformerFactory transformerFactory = TransformerFactory.newInstance();       
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(Path));
    transformer.transform(source, result); 

Although I indicated

transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 

XML is not indented properly, I would like to have XML in the state as before (except for the changes that were made)

Any help would be really appreciated.

Thank you very much in advance.

+4
source share
2 answers

You need to turn on "INDENT" and set the indent value for the transformer:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

See if this works.

+1
source

VTD-XML - Java, XML, .

, XPath XML. , .

import com.ximpleware.AutoPilot;
import com.ximpleware.ModifyException;
import com.ximpleware.NavException;
import com.ximpleware.TranscodeException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XMLModifier;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class TransformFile
{
    public static void main(String[] args)
        throws IOException, XPathParseException, ModifyException, NavException,
        XPathEvalException, TranscodeException
    {
        String inFilename = "input.xml";
        String outFilename = "output.xml";

        transform(inFilename, outFilename);
    }

    public static void transform(String inXmlFilePath, String outXmlFilePath)
        throws XPathParseException, ModifyException, XPathEvalException,
        NavException, IOException, TranscodeException
    {
        String xpath =
            "//Configuration[starts-with(@Name, 'Release')]/Tool[@Name = 'VCCLCompilerTool']/@BrowseInformation[. = '0']";

        OutputStream fos = new FileOutputStream(outXmlFilePath);
        try {
            VTDGen vg = new VTDGen();
            vg.parseFile(inXmlFilePath, false);
            VTDNav vn = vg.getNav();
            AutoPilot ap = new AutoPilot(vn);
            ap.selectXPath(xpath);

            XMLModifier xm = new XMLModifier(vn);

            int attrNodeIndex;
            while ((attrNodeIndex = ap.evalXPath()) != -1) {
                // An attribute value node always immediately follows an
                // attribute node.
                int attrValIndex = attrNodeIndex + 1;
                xm.updateToken(attrValIndex, "1");
            }

            xm.output(fos);
        }
        finally {
            fos.close();
        }
    }
}
+1

Source: https://habr.com/ru/post/1543576/


All Articles