【问题标题】:How to avoid error parsing xml with javascript?如何避免使用 javascript 解析 xml 时出错?
【发布时间】:2021-07-03 17:29:50
【问题描述】:

我正在尝试使用以下代码解析来自 NOAA 网站的 xml:

fetch("https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=EDDK&hoursBeforeNow=4",
{mode: 'no-cors'})
         .then(response => response.text())
         .then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
         .then(data => console.log(data));

但我收到此错误:

This page contains the following errors: error on line 1 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.
    

对使用 xml 有什么建议吗? 否则,我可以使用结果作为文本然后进行一些正则表达式搜索来避免问题吗?我只需要xml中的第一个data->METAR->raw_text

【问题讨论】:

    标签: javascript xml


    【解决方案1】:

    这里似乎是同样的问题:Javascript - CORS no response

    如果你测试,你可以看到response.text() 是空的。不确定该 API 是否打算公开,但似乎如果您想获取它,您必须从某个后端服务器而不是浏览器进行。

    【讨论】:

    猜你喜欢
    • 2019-05-11
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2012-06-01
    • 2016-06-12
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多