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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohanit46
    New Member
    • Jun 2010
    • 5

    #1

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

    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
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I recommend doing it server-side (more reliable than JavaScript), at least where fetching and basic processing is involved.

    Comment

    • rohanit46
      New Member
      • Jun 2010
      • 5

      #3
      well i appreciate your recommendation, but my task is strictly to do it on client side using javascript!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        just for clarification, the webservice and the website concerned are on the same server?

        Comment

        • rohanit46
          New Member
          • Jun 2010
          • 5

          #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

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            that’s due to the SOP (Same Origin Policy), a security mechanism for preventing XSS attacks.

            Comment

            • rohanit46
              New Member
              • Jun 2010
              • 5

              #7
              so how do i over come it?
              or what is the possible solution..?

              my code is like this -

              Code:
              <html>
              <head>
              
              <script type="text/javascript">
              var xmlDoc;
              
              if(window.XMLHttpRequest){
              	xhttp = new XMLHttpRequest();
              	}
              else{
              	xhttp=new ActiveXObject("Microsoft.XMLHTTP");
              	}
              	
              xhttp.open("GET","file:///C:/Documents%20and%20Settings/siddharthd/Desktop/javascript/xml/source.xml", false);
              xhttp.send("");
              xmlDoc=xhttp.responseXML;
              
              var x = xmlDoc.getElementsByTagName("tag");
              
              function display(){
              
              	tagid = x[0].getElementsByTagName("tag_id")[0].childNodes[0].nodeValue;
              	longitude = x[0].getElementsByTagName("longitude")[0].childNodes[0].nodeValue;
              	latitude = x[0].getElementsByTagName("latitude")[0].childNodes[0].nodeValue;
              	altitude = x[0].getElementsByTagName("altitude")[0].childNodes[0].nodeValue;
              	dateadded = x[0].getElementsByTagName("date_added")[0].childNodes[0].nodeValue;
              	datemodified = x[0].getElementsByTagName("date_modified")[0].childNodes[0].nodeValue;
              	name = x[0].getElementsByTagName("name")[0].childNodes[0].nodeValue;
              	value = x[0].getElementsByTagName("value")[0].childNodes[0].nodeValue;
              	
              	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;
              	document.getElementById("showdata").innerHTML=txt;	
              }
              </script>
              </head>
              
              <body onload="display()">
              <div id='showdata'></div>
              </body>
              </html>
              for a xml content which looks like this -

              Code:
              <tags>
              <tag>
              <tag_id>6</tag_id>
              <longitude>-97.721287186</longitude>
              <latitude>30.308577756</latitude>
              <altitude>189.752</altitude>
              <date_added>2010-05-24</date_added>
              <date_modified>2010-05-24</date_modified>
              <tag_data>
              <name_value>
              <name>id</name>
              <value>1</value>
              </name_value>
              </tag_data>
              </tag>
              </tags>
              Last edited by Dormilich; Jun 3 '10, 12:12 PM. Reason: Please use [code] tags when posting code

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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.

                Comment

                • rohanit46
                  New Member
                  • Jun 2010
                  • 5

                  #9
                  ok thanks for your help!

                  Comment

                  Working...