Hi my groovy script strips out an xml tag from a file and writes to a file.
import org.apache.commons.lang.RandomStringUtils
import groovy.util.XmlSlurper
inputFile = 'C:\\sample.xml'
outputFile = 'C:\\ouput.txt'
XMLTag='Details'
fileContents = new File(inputFile).getText('UTF-8')
def xmlFile=new XmlSlurper().parseText(fileContents)
def myPayload= new String(xmlFile.'**'.find{node-> node.name() == XMLTag} *.text().toString())
file = new File(outputFile)
w = file.newWriter()
w << myPayload.substring(1, myPayload.length()-1)
w.close()
My question is how do I write it so the it goes through an entire directory and performs it on multiple xml files and creates multiple output as at the moment it is hard coded. ('C:\\sample.xml' and 'C:\\ouput.txt')
Thanks
Leon
First, I would recommend that you take what you have and put it into a single function; it's good coding practrice and improves readabililty.
Now to executing the function on each xml file in a directory, you can use groovy's File.eachFileMatch() . For example, if you want to run it on each xml file in the current directory, you could do:
import org.apache.commons.lang.RandomStringUtils
import groovy.util.XmlSlurper
import static groovy.io.FileType.*
void stripTag(File inputFile, String outputFile) {
def XMLTag='Details'
fileContents = inputFile.getText('UTF-8')
def xmlFile=new XmlSlurper().parseText(fileContents)
def myPayload= new String(xmlFile.'**'.find{node-> node.name() == XMLTag}*.text().toString())
def file = new File(outputFile)
w = file.newWriter()
w << myPayload.substring(1, myPayload.length()-1)
w.close()
}
// This will match all files in the current directory with the file extension .xml
new File(".").eachFileMatch(FILES, ~/.*\.xml/) { File input ->
// Set the output file name to be <file_name>_out.txt
// Change this as you need
stripTag(input, input.name + "_out.txt")
}
If you want to, you can add reading in the directory from the command line as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.