Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2012, 12:14 PM   PM User | #1
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
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
File Type: txt info.txt (50 Bytes, 55 views)
juanp_1982 is offline   Reply With Quote
Old 06-18-2012, 01:42 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
What browser are you using ?
The code presented does not
work in IE.
DaveyErwin is offline   Reply With Quote
Old 06-18-2012, 04:04 PM   PM User | #3
MarPlo
Regular Coder

 
Join Date: Mar 2011
Posts: 145
Thanks: 0
Thanked 20 Times in 20 Posts
MarPlo is an unknown quantity at this point
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 .
__________________
MarPlo is offline   Reply With Quote
Old 06-18-2012, 10:38 PM   PM User | #4
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
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
juanp_1982 is offline   Reply With Quote
Old 06-25-2012, 08:27 AM   PM User | #5
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
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>
');
juanp_1982 is offline   Reply With Quote
Old 06-25-2012, 02:26 PM   PM User | #6
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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 ...
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
juanp_1982 (06-25-2012)
Old 06-25-2012, 09:23 PM   PM User | #7
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
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..
juanp_1982 is offline   Reply With Quote
Old 06-25-2012, 10:42 PM   PM User | #8
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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>
DaveyErwin is offline   Reply With Quote
Old 06-25-2012, 11:54 PM   PM User | #9
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
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;
                }
juanp_1982 is offline   Reply With Quote
Old 06-26-2012, 12:34 AM   PM User | #10
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Old 06-26-2012, 02:23 AM   PM User | #11
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
great!!!!!!!!!!!!!!!!!!!!!!! :-) finally my page is working as expected!!!!! :-)
juanp_1982 is offline   Reply With Quote
Old 06-26-2012, 03:31 AM   PM User | #12
juanp_1982
New Coder

 
Join Date: Oct 2011
Location: Toronto
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
juanp_1982 is an unknown quantity at this point
Quote:
Originally Posted by Albert343 View Post
Here is an example with a function that loads XML content from external file in javascript.
where?
juanp_1982 is offline   Reply With Quote
Reply

Bookmarks

Tags
"xml file", "xml string"

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:19 AM.


Advertisement
Log in to turn off these ads.