AJAX with XML Example in PHP
🌐 AJAX with XML Example in PHP
AJAX can also work with XML data instead of JSON. In this example, we’ll fetch XML from PHP and parse it in JavaScript to dynamically update the webpage.
1️⃣ XML Data (users.xml)
2️⃣ PHP Script to Serve XML (fetch_xml.php)
✅ This PHP script generates XML content dynamically.
3️⃣ HTML + JavaScript to Fetch and Parse XML
✅ The responseXML property of XMLHttpRequest automatically parses XML into a DOM object.
4️⃣ Key Points
-
Set
Content-Type: text/xmlin PHP when returning XML. -
Use
responseXMLin JavaScript to parse XML automatically. -
Access nodes with
getElementsByTagName()and.childNodes[0].nodeValue. -
Useful for legacy systems or APIs that return XML instead of JSON.
-
Can be combined with PHP to dynamically generate XML from a database.
