May 27, 2021 XML DOM
The code in the load XML documentation can be stored in a function.
To make the code on the previous page easy to maintain (check the old browser), it should be written as a function:
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
The above functions can be stored in the section of the HTML page and called from the script on the page.
The functions described above are used for all XML document instances in this tutorial!
To make the above code easier to maintain to ensure that the same code is used on all pages, we store the function in an external file.
The file name is "loadxmldoc .js" and the head section in the HTML page is loaded. The script on the page then calls the loadXMLDoc() function.
The following example uses the loadXMLDoc() function to load the .xml:
How to get data from an XML file is explained in the next chapter.
To make the code on the previous page easy to maintain (check the old browser), it should be written as a function:
function loadXMLString(txt)
{
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}
return xmlDoc;
}
The above functions can be stored in the section of the HTML page and called from the script on the page.
The functions described above are used for all XML string instances in this tutorial!
We've stored the loadXMLString() function in a file called "loadxmlstring.js" file.