XmlSlurper and http.get in groovy
I first save a txt file using http.get:
http.get(path: path,
contentType: TEXT,
query: [id:dapId, instance:alias, format:'xml', file:portalFile]) {resp, reader ->
println "response status: ${resp.statusLine}"
开发者_JAVA百科println 'Headers: -----------'
resp.headers.each { h ->
println " ${h.name} : ${h.value}"
}
new File(outputFileName).withWriter{out -> out << reader}
}
And then use the newly created file in outputFileName in XmlSlurper().parse, as below:
def inputFile = new File(outputFileName)
def domain = new XmlSlurper().parse(inputFile)
But I get an error when doing new XmlSlurper().parse(inputFile):
Caught: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
I noticed that the textfile outputFileName which is being created with http.get seems to be an HTML file and not an XML file. So I copied and pasted the XML code which it is supposed to contain into outputFileName, skipped the first part of the code and only ran the XmlSlurper().parse() bit and it worked.
Is outputFileName supposed to be an xml file? It has lots of HTML tags.
Thanks in advance! :D
HTML != XML. Your HTML file probably is not valid XML. Therefore, the XML parser fails during parsing. Are you sure that the file created from the http GET is a valid XML file?
精彩评论