簡體   English   中英

Groovy腳本,用於查找和替換XML文件中的字符

[英]Groovy script to find and replace characters in a XML file

我有下面的groovy腳本片段,我想替換xml文件列表中的一些字符,我該怎么做?

println "Remove Invisible characters in CustomMetadata"
def customMetadata = ant.fileScanner {
    fileset(dir: '${target.dir}') {
        include(name: 'customMetadata/*.md')
    }
}
// m is the file 
for (m in customMetadata) {
  //Want to get a content of the file and replace if there's any specified characters
  println("Found file $m")

}

如果您需要替換整個文件中的字符,只需使用

def content = new File('[your file name]').text

使用replaceAll()通過正則表達式替換您的字符,並將文件寫回

new File('[your file name]').write(content)

為了替換不必要的空格,這應該起作用。

一種“更清潔”的解決方案是解析文件,替換xml內容中的字符,然后將其寫回。 這更加復雜,並且可能導致XML名稱空間出現一些問題。 要嘗試一下,請搜索XMLSlurperXMLParserhttp : //www.groovy-lang.org/processing-xml.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM