how to load a XML file or a XML string in memory in JavaScript
Hi Every Body!!
I'm learning about XML, JavaScript and Ajax and I'm having a problem with XML and JavaScript. A PHP script is creating a XML file or/and string on the fly with data from the Data Base, this XML file is validate it against a XML Schema that I created so so far I know that the XML file or/and string is valid.
for a XML file I'm using this two lines
Code:
var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("info.xml");
Hi,
Here is an example with a function that loads XML content from external file in javascript.
Code:
// Ajax function, gets the content of an xml file and stores it in XML DOM
function getXML_file(xml_file) {
// www.coursesweb.net/ajax/
// if browser suports XMLHttpRequest
if (window.XMLHttpRequest) {
// Cretes a instantce of XMLHttpRequest object
xhttp = new XMLHttpRequest();
}
else {
// for IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// sets and sends the request for calling "xml_file"
xhttp.open("GET", xml_file ,false);
xhttp.send(null);
// sets and returns the content as XML DOM
xmlDoc = xhttp.responseXML;
return xmlDoc;
}
// XML file
var file_xml = 'xml_file.xml';
// calls the "getXML_file()" function "file_xml"
var xml_dom = getXML_file(file_xml);
For more details and examples, see this tutorial: Ajax and XML .
Hi,
Here is an example with a function that loads XML content from external file in javascript.
Code:
// Ajax function, gets the content of an xml file and stores it in XML DOM
function getXML_file(xml_file) {
// www.coursesweb.net/ajax/
// if browser suports XMLHttpRequest
if (window.XMLHttpRequest) {
// Cretes a instantce of XMLHttpRequest object
xhttp = new XMLHttpRequest();
}
else {
// for IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// sets and sends the request for calling "xml_file"
xhttp.open("GET", xml_file ,false);
xhttp.send(null);
// sets and returns the content as XML DOM
xmlDoc = xhttp.responseXML;
return xmlDoc;
}
// XML file
var file_xml = 'xml_file.xml';
// calls the "getXML_file()" function "file_xml"
var xml_dom = getXML_file(file_xml);
For more details and examples, see this tutorial: Ajax and XML .
thanks so much, MarPlo
thanks to you I have advance a little more today :-) however it's still no working,
before the javaScript code didn't run, (I said this because I used a couple of alert that didn't show anything) and now these alert are showing something in Firefox.
(I'm working with Firefox and Chrome)
I'm posting the two JavaScript file that I'm using the one that is run in a PHP script and the one that is run in a HTML file (the main page).
so I was wondering if you can see the error. The XML file is there, in the same folder as all the codes.
after posting I'll start reading the tutorial that you gave me thanks again for your help
Hi Guys, I'm still having problems, I'm trying to load a well formatted XML string into a dom but the browsers are complaning about the XML string, could you tell me what I'm doing wrong? please (I discarded the file idea because I think it's no efficient) thanks once again
Your xml is well formed but
your javascript does Not
produce a string.
look …
alert("hello
goodbye")
fails but ...
alert("hello "+
"goodbye")
succedes.
you need something like this …
xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?> '+
'<query xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+ 'xsi:noNamespaceSchemaLocation="Visitor.xsd">'+
'<row vID="2">'+
'<vFname>Isaac</vFname>'+
'<vLName>Newton</vLName>'+
and so on ...
the string works now, and the Dom document with XML is created without problems (that what I think) however when I try to access one of its values or names I get a "undefined". do you see whats wrong???/
Code:
...
var xmlDoc = loadXMLString(<?php echo "'$sInfo'"; ?>);
var rowXML = xmlDoc.getElementsByTagName("vFname");
alert(rowXML.nodeValue); //undefined
alert(rowXML.nodeName); //undefined
alert(rowXML.length); //1
...
...
...
function loadXMLString(txtXML) {
var xmlDoc;
if (window.DOMParser){
parser=new DOMParser();
xmlDoc=parser.parseFromString(txtXML,"text/xml");
}else{ // Internet Explorer
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(txtXML);
}
return xmlDoc;
}