groovy 中的 XmlSlurper 和 http.get

发布于 2024-12-02 07:11:07 字数 1082 浏览 0 评论 0原文

我首先使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-12-09 07:11:07

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文