By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,043 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,043 IT Pros & Developers. It's quick & easy.

reading xml values through javascript

P: 8
hi
I am trying to get xml values into javascript I have been trying to get xml values through javascript and it is working fine in IE but not in firefox.
here are the javascript code
Expand|Select|Wrap|Line Numbers
  1. var xmlreq = false;
  2. //xmlreq =document.implementation.createDocument("","",null);
  3. xmlreq = new ActiveXObject("Microsoft.XMLDOM");
  4. var xmlFile="/val/valid.xml";
  5. var vale="";
  6. xmlreq.async="false";
  7. xmlreq.onreadystatechange=verify();
  8. xmlreq.load(xmlFile);
  9.  var xmlDoc =xmlreq.documentElement;
  10.  
  11. vale=xmlDoc.getElementsByTagName('firstcheck')[0].firstChild.nodeValue;
  12.  
  13. // i need to get the values from xml to this variable "vale" 
  14.  
  15. function verify() { 
  16.  if (xmlreq.readyState != 4) { 
  17. return false; 
  18.  } 
  19.  alert(xmlreq.readyState);
  20. }
my xml file is, "valid.xml"

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <validationcheck>
  3.     <firstcheck>check</firstcheck>
  4.     <firstcheck>venki</firstcheck>
  5.  
  6. </validationcheck>
The above code is working fine with IE .I also tried to work with firefox with these code

xmlreq =document.implementation.createDocument("","",null );
but after load,
xmlreq.load(xmlFile);
var xmlDoc =xmlreq.documentElement;
if i alert xmlDoc
it gives null , and error is xmldoc has no properties.please help me
Dec 4 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
See this tutorial.
Dec 4 '08 #2

gits
Expert Mod 5K+
P: 5,390
especially have an eye on the 'async' property :) in the mentioned tutorial ...
Dec 4 '08 #3

P: 8
Hi
Thank u for u suggestion .I worked out in another method and works here are solution ,

function getSalesTotal()

{
url = "/corporate/commonjs/YUI_js/validation/valid.xml";
// AJAX code for Mozilla, Safari, Opera etc.
if (window.XMLHttpRequest) {


xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = xmlhttpChange;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
// AJAX code for IE
else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp)
{
xmlhttp.onreadystatechange = xmlhttpChange;
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
}
}

function xmlhttpChange()
{
first=1;
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
xmlhttp.responseXML.getElementsByTagName('required ')[select].firstChild.data
}
}
}
getSalesTotal();
xmlhttp.async="false";
xmlhttpChange();
the results are obtained through
xmlhttp.responseXML.getElementsByTagName('required ')[select].firstChild.data
in the above code [select] value will be eithe 0 or 1.I need to get either english or japanese words from xml.so depending upon browser language i will get the values from the xml file.
Dec 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.