如何解析此特定的XML数据?

时间:2020-09-29 05:06:43

标签: javascript node.js

我正在尝试解析此XML,但无法提取该文件中的所有“

”文本,有人可以告诉我我做错了什么吗?

这是我要请求的HTTP / GET,

REQUEST_LINK :“https://cdnsecakmi.kaltura.com/api_v3/index.php/service/caption_captionAsset/action/serve/captionAssetId/0_o7nr2wfk/v/2/ks/djJ8MjAxMTUzMXyLXeiobMjq3pQ7i9wYSlwZHhVUdAP8C6IYMqJGpcDqOsBc4X5e0rTARtFc6ysPSkDTq_u7qfycIGeJquoL_O_3MlBxOn9rBGgQBZXeeAQrcd8tmZqjZo2h5bQVGtQXgFHVc7OyICn-cY2lHa4ZjRp6w2ueyIRqyNp9pHZ5onX2Z6gfjyNR4MubV3V6Qwfa3e0hbRdAoCap1dgO2QIawMKi /.dfxp”

const get_religion_notes = new XMLHttpRequest();
get_religion_notes.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
       console.log(get_religion_notes.responseXML)
       
       let parser = new DOMParser();
       xmlDoc = parser.parseFromString(get_religion_notes,"text/xml");
       console.log(document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0])
       document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0]; //Undefined why is that?
    }
};
get_religion_notes.open("GET", "https://cdnsecakmi.kaltura.com/api_v3/index.php/service/caption_captionAsset/action/serve/captionAssetId/0_o7nr2wfk/v/2/ks/djJ8MjAxMTUzMXyLXeiobMjq3pQ7i9wYSlwZHhVUdAP8C6IYMqJGpcDqOsBc4X5e0rTARtFc6ysPSkDTq_u7qfycIGeJquoL_O_3MlBxOn9rBGgQBZXeeAQrcd8tmZqjZo2h5bQVGtQXgFHVc7OyICn-cY2lHa4ZjRp6w2ueyIRqyNp9pHZ5onX2Z6gfjyNR4MubV3V6Qwfa3e0hbRdAoCap1dgO2QIawMKi/.dfxp", true);
get_religion_notes.send();

1 个答案:

答案 0 :(得分:-1)

一切都正确。

只要确保您使用的responseText解析器需要文本xml而不是xml本身即可。

const get_religion_notes = new XMLHttpRequest();
get_religion_notes.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
       console.log(get_religion_notes.responseText)
       
       let parser = new DOMParser();
       xmlDoc = parser.parseFromString(get_religion_notes.responseText,"text/xml");
       console.log(document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0])
       document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("p")[0]; //Undefined why is that?
    }
};
get_religion_notes.open("GET", "https://cdnsecakmi.kaltura.com/api_v3/index.php/service/caption_captionAsset/action/serve/captionAssetId/0_o7nr2wfk/v/2/ks/djJ8MjAxMTUzMXyLXeiobMjq3pQ7i9wYSlwZHhVUdAP8C6IYMqJGpcDqOsBc4X5e0rTARtFc6ysPSkDTq_u7qfycIGeJquoL_O_3MlBxOn9rBGgQBZXeeAQrcd8tmZqjZo2h5bQVGtQXgFHVc7OyICn-cY2lHa4ZjRp6w2ueyIRqyNp9pHZ5onX2Z6gfjyNR4MubV3V6Qwfa3e0hbRdAoCap1dgO2QIawMKi/.dfxp", true);
get_religion_notes.send();