<?xml version="1.0"?>
<design>
<process>
<variable id="V1" visible="true" />
<variable id="V2" visible="false" />
<variable id="V3" visible="false" />
</process>
</design>
And the script to change all XML files in the current directory.
I found it easier to write and debug it than using a mix of java and XSL.
def basedir = new File( ".")
// Create a directory for patched files
new File("patched").mkdir()
// Get files with ".xml" extension
files = basedir.listFiles().grep(~/.*\.xml$/)
// Iterate on the files
files.each {
patchXML(it)
}
def patchXML(file) {
println "-------------------"
println "Patching $file.name"
def design = new XmlParser().parse(file)
def modified = false
for (variable in design.process.variable) {
switch (variable.@id) {
case "V1":
case "V2":
if (variable.@visible == "false") {
variable.@visible = "true"
modified = true
}
}
}
if (modified) {
new File("patched/$file.name").withPrintWriter() {
new XmlNodePrinter(it).print(design)
}
println "Patched $file.name is in \"patched\" directory"
} else {
println "$file.name was not modified"
}
println "-------------------"
}
Aucun commentaire:
Enregistrer un commentaire