reading xml values through javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shareme
    New Member
    • Dec 2008
    • 8

    reading xml values through javascript

    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
    Code:
    var xmlreq = false;
    //xmlreq =document.implementation.createDocument("","",null);
    xmlreq = new ActiveXObject("Microsoft.XMLDOM");
    var xmlFile="/val/valid.xml";
    var vale="";
    xmlreq.async="false";
    xmlreq.onreadystatechange=verify();
    xmlreq.load(xmlFile);
     var xmlDoc =xmlreq.documentElement;
    
    vale=xmlDoc.getElementsByTagName('firstcheck')[0].firstChild.nodeValue;
    
    // i need to get the values from xml to this variable "vale" 
    
    function verify() { 
     if (xmlreq.readyState != 4) { 
    return false; 
     } 
     alert(xmlreq.readyState);
    }
    my xml file is, "valid.xml"

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <validationcheck>
    	<firstcheck>check</firstcheck>
    	<firstcheck>venki</firstcheck>
    	
    </validationcheck>
    The above code is working fine with IE .I also tried to work with firefox with these code

    xmlreq =document.imple mentation.creat eDocument("","" ,null);
    but after load,
    xmlreq.load(xml File);
    var xmlDoc =xmlreq.documen tElement;
    if i alert xmlDoc
    it gives null , and error is xmldoc has no properties.plea se help me
    Last edited by acoder; Dec 4 '08, 02:54 PM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    See this tutorial.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      especially have an eye on the 'async' property :) in the mentioned tutorial ...

      Comment

      • shareme
        New Member
        • Dec 2008
        • 8

        #4
        Accessing xml through javascript

        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.XMLHttp Request) {


        xmlhttp = new XMLHttpRequest( );
        xmlhttp.onready statechange = xmlhttpChange;
        xmlhttp.open("G ET", url, true);
        xmlhttp.send(nu ll);
        }
        // AJAX code for IE
        else if (window.ActiveX Object) {
        xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
        if (xmlhttp)
        {
        xmlhttp.onready statechange = xmlhttpChange;
        xmlhttp.open("G ET", url, true);
        xmlhttp.send();
        }
        }
        }

        function xmlhttpChange()
        {
        first=1;
        if (xmlhttp.readyS tate == 4) {
        if (xmlhttp.status == 200) {
        xmlhttp.respons eXML.getElement sByTagName('req uired')[select].firstChild.dat a
        }
        }
        }
        getSalesTotal() ;
        xmlhttp.async=" false";
        xmlhttpChange() ;
        the results are obtained through
        xmlhttp.respons eXML.getElement sByTagName('req uired')[select].firstChild.dat a
        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.

        Comment

        Working...