Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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");
    but the script stop in the second line.

    to load a XML string I have this code

    Code:
              parser=new DOMParser();
              xmlDoc=parser.parseFromString(<?php echo '"' . $sInfo . '"';?>,"text/xml");
    the problem is that Neither work, could any one give me a hint about what I'm doing wrong???
    the String and the file are identical

    I'm attaching the XML file, I'm testing the page in Firefox and Chrome


    thanks so much in advance for your help
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    What browser are you using ?
    The code presented does not
    work in IE.

  • #3
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    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 .

  • #4
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MarPlo View Post
    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

  • #5
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

    Code:
    if (window.DOMParser){
                                       parser=new DOMParser();
                                       xmlDoc=parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?>
    Uncaught SyntaxError: Unexpected token ILLEGAL
    <query xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Visitor.xsd">
      <row vID="2">
        <vFname>Isaac</vFname>
        <vLName>Newton</vLName>
        <vAge>55</vAge>
        <vEmail>isaacnewton@gmail.com</vEmail>
        <vPassword>WQwqWQer#$</vPassword>
        <vPhone>6137828782</vPhone>
        <vSex>Male</vSex>
        <vCountry>England</vCountry>
        <vAddress>234 Wellington Street Ottawa, Ontario, Canada K1A 0G9</vAddress>
        <datereg>2012-06-22</datereg>
        <timereg>20:45:41</timereg>
        <IPaddress>174.91.144.104</IPaddress>
      </row>
    </query>
    ',"text/xml");
                                  }else{ // Internet Explorer
                                       xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                                       xmlDoc.async=false;
                                       xmlDoc.loadXML('<?xml version="1.0" encoding="UTF-8"?> //<------here
    <query xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Visitor.xsd">
      <row vID="2">
        <vFname>Isaac</vFname>
        <vLName>Newton</vLName>
        <vAge>55</vAge>
        <vEmail>isaacnewton@gmail.com</vEmail>
        <vPassword>WQwqWQer#$</vPassword>
        <vPhone>6137828782</vPhone>
        <vSex>Male</vSex>
        <vCountry>England</vCountry>
        <vAddress>234 Wellington Street Ottawa, Ontario, Canada K1A 0G9</vAddress>
        <datereg>2012-06-22</datereg>
        <timereg>20:45:41</timereg>
        <IPaddress>174.91.144.104</IPaddress>
      </row>
    </query>
    ');

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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 ...

  • Users who have thanked DaveyErwin for this post:

    juanp_1982 (06-25-2012)

  • #7
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks to your suggestion I was able to fix the string like this

    PHP Code:
    $sInfo str_replace(array("\r""\n"), ''$sInfo); 
    ,
    Last edited by juanp_1982; 06-25-2012 at 10:55 PM.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <script type="text/javascript">
    var myXmlText = '<?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>'+
        '<vAge>55</vAge>'+
        '<vEmail>isaacnewton@gmail.com</vEmail>'+
        '<vPassword>WQwqWQer#$</vPassword>'+
        '<vPhone>6137828782</vPhone>'+
        '<vSex>Male</vSex>'+
        '<vCountry>England</vCountry>'+
        '<vAddress>234 Wellington Street Ottawa, Ontario, Canada K1A 0G9</vAddress>'+
        '<datereg>2012-06-22</datereg>'+
        '<timereg>20:45:41</timereg>'+
        '<IPaddress>174.91.144.104</IPaddress>'+
      '</row>'+
    '</query>';
    function txtToXmlDoc(txt){
    	var xmlDoc;
    	if (window.ActiveXObject){
      		xmlDoc=new ActiveXObject("Msxml2.DOMDocument.6.0");
      		xmlDoc.loadXML(txt);   
      	}else{
      		parser=new DOMParser();
      		xmlDoc=parser.parseFromString(txt,"text/xml");
      	} 
    	return xmlDoc;
    }
    xml = txtToXmlDoc(myXmlText);
    alert(xml.getElementsByTagName("vPassword")[0].firstChild.nodeValue)
    </script>
    
    </body>
    </html>

  • #9
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks!

    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;
                    }

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <script type="text/javascript">
    var myXmlText = <?php echo "'$sInfo'"; ?>;
    function txtToXmlDoc(txt){
    	var xmlDoc;
    	if (window.ActiveXObject){
      		xmlDoc=new ActiveXObject("Msxml2.DOMDocument.6.0");
      		xmlDoc.loadXML(txt);   
      	}else{
      		parser=new DOMParser();
      		xmlDoc=parser.parseFromString(txt,"text/xml");
      	} 
    	return xmlDoc;
    }
    xml = txtToXmlDoc(myXmlText);
    alert(xml.getElementsByTagName("vPassword")[0].firstChild.nodeValue);
    
    var rowXML = xml.getElementsByTagName("vFname");
         alert(rowXML[0].firstChild.nodeValue);             
         alert(rowXML[0].nodeName);              
         alert(rowXML.length);                    
    
    </script>
    
    </body>
    </html>
    Last edited by DaveyErwin; 06-26-2012 at 12:37 AM.

  • #11
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    great!!!!!!!!!!!!!!!!!!!!!!! :-) finally my page is working as expected!!!!! :-)

  • #12
    New Coder
    Join Date
    Oct 2011
    Location
    Toronto
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Albert343 View Post
    Here is an example with a function that loads XML content from external file in javascript.
    where?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •