2011-11-10 24 views
2

未格式化的XML是我們構建中的一個問題。我想包括XML文檔的樣式檢查,主要是尋找可憐的縮進。如何執行XML樣式檢查?

什麼是最好的工具使用? (構建使用Ant)。

回答

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

感謝+1 - 但是,我要檢查格式化,而不是格式化。當然,我可以使用相同的方法(寫一些東西來檢查diff格式==原始),但我正在尋找一些已經存在的東西 - 比如使用checkstyle for xml或somesuch的方法。 – Synesso

0

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

這是你如何與螞蟻整合呢:

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