【问题标题】:How to perform XML style checking?如何执行 XML 样式检查?
【发布时间】:2011-11-10 05:51:23
【问题描述】:

未格式化的 XML 是我们构建中的一个问题。我想包括对 XML 文档的样式检查,主要是寻找不良缩进。

什么是最好的工具? (构建使用 Ant)。

【问题讨论】:

    标签: build checkstyle xml-formatting


    【解决方案1】:

    您可以编写一个自动转换并因此缩进您的 XML 的类。然后只需在 Ant 中指定应该在哪些 XML 文件上运行。

    让你开始的东西:

    String filePath = "test.xml";
    String outputFilePath = "testOut.xml";
    
    File xmlFile = new File(filePath);
    File outputXmlFile = new File(outputFilePath);
    
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
    
    StreamSource ss = new StreamSource(xmlFile);
    StreamResult sr = new StreamResult(outputXmlFile);      
    
    transformer.transform(ss, sr);
    

    【讨论】:

    • 谢谢 +1 - 但是,我想检查格式,而不是重新格式化。当然,我可以使用相同的方法(写一些东西来检查 diff formatted == original),但我正在寻找已经存在的东西 - 例如使用 checkstyle for xml 或类似的方法。
    【解决方案2】:

    检查 XCOP:http://www.yegor256.com/2017/08/29/xcop.html

    这就是你将它与 ant 集成的方式:

    <project>
      <target name="xcop">
        <apply executable="xcop" failonerror="true">
          <fileset dir=".">
            <include name="**/*.xml"/>
            <include name="**/*.xsd"/>
            <exclude name="target/**/*"/>
            <exclude name=".idea/**/*"/>
          </fileset>
        </apply>
      </target>
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 2014-01-14
      • 1970-01-01
      相关资源
      最近更新 更多