groovy 中的 XmlSlurper 和 http.get
我首先使用http.get保存一个txt文件:
http.get(path: path,
contentType: TEXT,
query: [id:dapId, instance:alias, format:'xml', file:portalFile]) {resp, reader ->
println "response status: ${resp.statusLine}"
println 'Headers: -----------'
resp.headers.each { h ->
println " ${h.name} : ${h.value}"
}
new File(outputFileName).withWriter{out -> out << reader}
}
然后在XmlSlurper().parse中的outputFileName中使用新创建的文件,如下所示:
def inputFile = new File(outputFileName)
def domain = new XmlSlurper().parse(inputFile)
但是在执行new XmlSlurper().parse(inputFile)时出现错误:
Caught: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId.
我注意到使用http.get 创建的文本文件outputFileName 似乎是一个HTML 文件而不是XML 文件。因此,我将应该包含的 XML 代码复制并粘贴到了 outputFileName 中,跳过了代码的第一部分,只运行了 XmlSlurper().parse() 位,结果成功了。
outputFileName 应该是一个 xml 文件吗?它有很多 HTML 标签。
提前致谢! :D
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}"
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTML!= XML。您的 HTML 文件可能不是有效的 XML。因此,XML 解析器在解析过程中会失败。您确定从 http GET 创建的文件是有效的 XML 文件吗?
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?