【问题标题】:Getting Premature end of file when building an XML object from a URL从 URL 构建 XML 对象时文件过早结束
【发布时间】:2018-04-14 14:12:43
【问题描述】:

我正在尝试运行以下代码 val podcastXml = XML.load(new URL(feed)),其中有问题的提要是 https://fourfingerdiscount.podbean.com/feed/

我可以在我的浏览器中加载提要,但我得到了一个 error: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file. 当我尝试针对它运行该代码时。

有趣的是,当我尝试在浏览器中curl 提要 URL 时,它是空的。

知道我在这里做错了什么吗?我觉得我缺少一个配置选项。

另外值得一提的是,一些提要可以正常工作,例如http://maximumfun.org/feeds/are.xml

【问题讨论】:

    标签: java xml scala sax


    【解决方案1】:

    从 HTTPS URL 读取并不像 new URL(feed) 那样直接。它不会那么容易地获得所需的 XML 内容,而是得到一个空响应和SAXParseException

    但是可以检索数据,其中一种方法是旧的HttpsUrlConnection。但也有一些包装器库允许促进它,例如scalaj-http。有了它,您可以进行如下操作:

    import scalaj.http.Http
    
    val url = "https://fourfingerdiscount.podbean.com/feed/"
    val xml = Http(url).execute(XML.load).body
    // xml is a parsed scala.xml.Elem with the contents you were looking for
    

    【讨论】:

    • 这太棒了。谢谢。
    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2012-04-28
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多