JS का AJAX XML
In this page:
const xmlDoc = xhr.responseXML;
const nodes = xmlDoc.getElementsByTagName("tag");
nodes[0].childNodes[0].nodeValue;
XMLHttpRequest से responseXML पढ़ना
XMLHttpRequest एक XML response body को अपने आप एक Document object में parse कर देता है, जो xhr.responseXML के रूप में उपलब्ध होता है, बशर्ते server ने एक XML-appropriate Content-Type header भेजा हो -- fetch के उलट, कोई manual parsing step नहीं चाहिए।
उदाहरण: Reading responseXML from XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.onload = () => console.log(xhr.responseXML); // parsed automatically
xhr.open("GET", "data.xml");
xhr.send();
fetch और DOMParser से XML Parse करना
fetch के पास responseXML जैसा कुछ नहीं है -- एक XML response को response.text() से plain text के रूप में पढ़ना होगा, फिर new DOMParser().parseFromString(text, 'text/xml') इस्तेमाल करके manually एक Document में parse करना होगा।
उदाहरण: Parsing XML with fetch and DOMParser
// Define the method `fetch` taking `"data.xml"`
// Define the method `fetch` taking `"data.xml"`
fetch("data.xml")
// On success, run this with the resolved value as `response`
// On success, run this with the resolved value as `response`
.then(response => response.text())
// On success, run this with the resolved value as `text`
// On success, run this with the resolved value as `text`
.then(text => new DOMParser().parseFromString(text, "text/xml"))
// On success, run this with the resolved value as `doc`
// On success, run this with the resolved value as `doc`
.then(doc => console.log(doc));
एक Parsed XML Document में Navigate करना
एक बार parse हो जाने पर (responseXML या DOMParser के ज़रिए), एक XML response किसी भी दूसरे DOM document की तरह व्यवहार करता है -- querySelector(), querySelectorAll(), और getElementsByTagName() सभी बिल्कुल वैसे ही काम करते हैं जैसे किसी HTML page पर करते हैं।
उदाहरण: Navigating a Parsed XML Document
// Declare the constant `xmlText`, set to "<root><item>Test</item></root>"
// Declare the constant `xmlText`, set to "<root><item>Test</item></root>"
const xmlText = "<root><item>Test</item></root>";
// Declare the constant `doc` as a new `DOMParser` instance
// Declare the constant `doc` as a new `DOMParser` instance
const doc = new DOMParser().parseFromString(xmlText, "text/xml");
// Print `doc.querySelector("item").textContent` to the console
// Print `doc.querySelector("item").textContent` to the console
console.log(doc.querySelector("item").textContent);
XML और JSON Responses की तुलना
किसी मौजूदा XML-based endpoint के response को JSON में बदलना नए features के लिए अक्सर करने लायक होता है, क्योंकि JSON अपने आप plain JavaScript objects/arrays में parse हो जाता है, XML को चाहिए मैनुअल DOM-style navigation की कोई ज़रूरत नहीं -- पर मौजूदा XML endpoints को पढ़ना अभी भी एक असली, कभी-कभी ज़रूरी काम है।
उदाहरण: Comparing XML and JSON Responses
// Declare the constant `doc` as a new `DOMParser` instance
// Declare the constant `doc` as a new `DOMParser` instance
const doc = new DOMParser().parseFromString("<root><item>1</item></root>", "text/xml");
// Print `doc.getElementsByTagName("item")[0].textContent` to the console
// Print `doc.getElementsByTagName("item")[0].textContent` to the console
console.log(doc.getElementsByTagName("item")[0].textContent);
// Print `JSON.parse('{"item": 1}').item` to the console
// Print `JSON.parse('{"item": 1}').item` to the console
console.log(JSON.parse('{"item": 1}').item);
एक Practical XML AJAX Example
टुकड़ों को मिलाकर: एक RSS-style XML feed को fetch करना, DOMParser से इसे parse करना, और headlines की एक साधारण list बनाने के लिए इसके दोहराए गए <item> elements पर loop करना -- एक असली, पूरा उदाहरण है कि किसी XML-based AJAX endpoint को शुरू से आखिर तक कैसे consume करें।
उदाहरण: A Practical XML AJAX Example
// Define the method `fetch` taking `"feed.xml"`
// Define the method `fetch` taking `"feed.xml"`
fetch("feed.xml")
// On success, run this with the resolved value as `r`
// On success, run this with the resolved value as `r`
.then(r => r.text())
// On success, run this with the resolved value as `text`
// On success, run this with the resolved value as `text`
.then(text => {
// Declare the constant `doc` as a new `DOMParser` instance
// Declare the constant `doc` as a new `DOMParser` instance
const doc = new DOMParser().parseFromString(text, "text/xml");
// Call `doc.querySelectorAll("item").forEach(item => console.log(item.textContent))`
// Call `doc.querySelectorAll("item").forEach(item => console.log(item.textContent))`
doc.querySelectorAll("item").forEach(item => console.log(item.textContent));
});
- किसी XML response पर response.json() इस्तेमाल करने की कोशिश करना, जो fail होता है क्योंकि body valid JSON नहीं है -- XML responses को text की तरह पढ़ना और अलग से parse करना ज़रूरी है।
- यह भूल जाना कि responseXML (XMLHttpRequest पर) सिर्फ तब automatically populate होता है जब server XML के लिए सही Content-Type header भेजता है।
- resulting document में किसी parser error को check न करना, जिसे DOMParser एक catchable exception throw करने के बजाय एक special element की तरह insert करता है।
- अगर server का Content-Type header XML बताता है तो XMLHttpRequest एक XML response को automatically responseXML में parse कर देता है।
- fetch के लिए, एक XML response को response.text() से पढ़ना और फिर built-in DOMParser से manually parse करना ज़रूरी है।
- parse होने के बाद, एक XML response को किसी HTML document पर इस्तेमाल होने वाले same DOM methods (querySelector, getElementsByTagName) से navigate किया जा सकता है।
XMLHttpRequest.responseXML और DOMParser API दोनों हर modern browser में supported हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML