JS AJAX XML
In this page:
const xmlDoc = xhr.responseXML;
const nodes = xmlDoc.getElementsByTagName("tag");
nodes[0].childNodes[0].nodeValue;
Reading responseXML from XMLHttpRequest
XMLHttpRequest automatically parses an XML response body into a Document object, available as xhr.responseXML, provided the server sent an XML-appropriate Content-Type header -- no manual parsing step required, unlike with fetch.
उदाहरण: Reading responseXML from XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.onload = () => console.log(xhr.responseXML); // parsed automatically
xhr.open("GET", "data.xml");
xhr.send();
Parsing XML with fetch and DOMParser
fetch does not have an equivalent to responseXML -- an XML response must be read as plain text with response.text(), then parsed manually into a Document using new DOMParser().parseFromString(text, 'text/xml').
उदाहरण: 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));
Navigating a Parsed XML Document
Once parsed (via either responseXML or DOMParser), an XML response behaves like any other DOM document -- querySelector(), querySelectorAll(), and getElementsByTagName() all work identically to how they work on an 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);
Comparing XML and JSON Responses
Converting an existing XML-based endpoint's response to JSON is often worth doing for new features, since JSON parses into plain JavaScript objects/arrays automatically, requiring none of the manual DOM-style navigation XML needs -- but reading existing XML endpoints remains a real, occasional necessity.
उदाहरण: 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);
A Practical XML AJAX Example
Combining the pieces: fetching an RSS-style XML feed, parsing it with DOMParser, and looping over its repeated <item> elements to build a simple list of headlines -- a realistic, complete example of consuming an XML-based AJAX endpoint end to end.
उदाहरण: 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));
});
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