【问题标题】:XML formatting in java - Mainting the state indentjava中的XML格式——维护状态缩进
【发布时间】:2014-06-06 15:06:34
【问题描述】:

我有一个 XML 文件,我打开并编辑节点中的一些属性,然后将其保存回来,但由于某种原因,保存的 XML 没有像以前那样正确缩进。

这是我保存 XML 文件的代码:

    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); 

虽然我已经指定了

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

XML 没有正确缩进,我希望 XML 保持之前的状态(所做的更改除外)

任何帮助将不胜感激。

非常感谢。

【问题讨论】:

  • 我的解决方案是否有效,如果是,您应该接受它作为答案。

标签: java xml xml-formatting


【解决方案1】:

您需要启用 'INDENT' 并​​为转换器设置缩进量:

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

看看这是否有效。

【讨论】:

    【解决方案2】:

    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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多