如何在js中从其他网页(url)获取xml的某一行?

How to get a certain line from xml from other webpage (url) in js?

const xhrRequest = new XMLHttpRequest();

xhrRequest.onload = function()
{
  dump(xhrRequest.responseXML.documentElement.nodeName);

  console.log(xhrRequest.responseXML.documentElement.nodeName);

  
  
}

xhrRequest.open("GET", "/website_url.xml")
xhrRequest.responseType = "document";
xhrRequest.send();

我正在尝试从一个页面请求一个 xml 页面,但我无法从 javascript 中的 xml 获取特定行。谢谢!

您可以使用此处的 AJAX http 请求轻松向其他页面发送请求:https://www.w3schools.com/js/js_ajax_intro.asp

这是一个示例函数:

function SendRequest(){
    let xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if(this.readyState == 4 && this.status == 200){
            // Success
        }
    };
    xmlhttp.open("GET", "example.com", true);
    xmlhttp.send();
}

现在,关于从 xml 文档中获取值,您可以使用 .getElementsByTagName()。请注意,这是一个元素数组,因此您必须附加一个索引,例如 [0]
这将进入 http 请求

onreadystatechange
if(this.readyState == 4 && this.status == 200){
    let xmlDocument = this.responseXML;
    console.log(xmlDocument.getElementsByTagName("TestTag")[0].childNodes[0].nodeValue);
}

因此,如果 xml 文档具有如下元素:

<TestTag>Hello</TestTag>

该函数将打印 Hello