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

need to read xml content from a web service onto a local machine

P: 5
i need to read xml content from a web service onto a local machine and parse the data using javascript.

While using XMLHttpRequest, it allows me to access xml content from anywhere on local machine. But i m not getting a clue as to how to access the content from a service
Any help appreciated
Jun 3 '10 #1
Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
I recommend doing it server-side (more reliable than JavaScript), at least where fetching and basic processing is involved.
Jun 3 '10 #2

P: 5
well i appreciate your recommendation, but my task is strictly to do it on client side using javascript!
Jun 3 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
just for clarification, the webservice and the website concerned are on the same server?
Jun 3 '10 #4

P: 5
well the script is on local machine..and i need to access the xml content which is on a server..

i have tried to parse the xml content on the local machine using XMLHttpRequest object and i was successful. But It is not working while trying to access from web service
Jun 3 '10 #5

Dormilich
Expert Mod 5K+
P: 8,639
that’s due to the SOP (Same Origin Policy), a security mechanism for preventing XSS attacks.
Jun 3 '10 #6

P: 5
so how do i over come it?
or what is the possible solution..?

my code is like this -

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript">
  5. var xmlDoc;
  6.  
  7. if(window.XMLHttpRequest){
  8.     xhttp = new XMLHttpRequest();
  9.     }
  10. else{
  11.     xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  12.     }
  13.  
  14. xhttp.open("GET","file:///C:/Documents%20and%20Settings/siddharthd/Desktop/javascript/xml/source.xml", false);
  15. xhttp.send("");
  16. xmlDoc=xhttp.responseXML;
  17.  
  18. var x = xmlDoc.getElementsByTagName("tag");
  19.  
  20. function display(){
  21.  
  22.     tagid = x[0].getElementsByTagName("tag_id")[0].childNodes[0].nodeValue;
  23.     longitude = x[0].getElementsByTagName("longitude")[0].childNodes[0].nodeValue;
  24.     latitude = x[0].getElementsByTagName("latitude")[0].childNodes[0].nodeValue;
  25.     altitude = x[0].getElementsByTagName("altitude")[0].childNodes[0].nodeValue;
  26.     dateadded = x[0].getElementsByTagName("date_added")[0].childNodes[0].nodeValue;
  27.     datemodified = x[0].getElementsByTagName("date_modified")[0].childNodes[0].nodeValue;
  28.     name = x[0].getElementsByTagName("name")[0].childNodes[0].nodeValue;
  29.     value = x[0].getElementsByTagName("value")[0].childNodes[0].nodeValue;
  30.  
  31.     txt="tag_id: " + tagid + "<br/>Longitude: " + longitude + "<br/>Latitude: " + latitude + "<br/>Altitude: " + altitude + "<br/>Date Added: " + dateadded + "<br/>Date Modified: " + datemodified + "<br/><br/>Name: " + name + "<br/>Value: " + value;
  32.     document.getElementById("showdata").innerHTML=txt;    
  33. }
  34. </script>
  35. </head>
  36.  
  37. <body onload="display()">
  38. <div id='showdata'></div>
  39. </body>
  40. </html>
  41.  
for a xml content which looks like this -

Expand|Select|Wrap|Line Numbers
  1. <tags>
  2. <tag>
  3. <tag_id>6</tag_id>
  4. <longitude>-97.721287186</longitude>
  5. <latitude>30.308577756</latitude>
  6. <altitude>189.752</altitude>
  7. <date_added>2010-05-24</date_added>
  8. <date_modified>2010-05-24</date_modified>
  9. <tag_data>
  10. <name_value>
  11. <name>id</name>
  12. <value>1</value>
  13. </name_value>
  14. </tag_data>
  15. </tag>
  16. </tags>
Jun 3 '10 #7

Dormilich
Expert Mod 5K+
P: 8,639
I guess, you would have to lower your browser’s security settings, but that’s beyond my scope.

besides, you try to access a local file, which is (strictly speaking) forbidden in JavaScript (again for security reasons). in IE you may use an ActiveX control to do it.
Jun 3 '10 #8

P: 5
ok thanks for your help!
Jun 3 '10 #9

Post your reply

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