parse responseText or responseXML

Last post 07-03-2009 12:56 AM by sunny74. 14 replies.

Sort Posts:

  • parse responseText or responseXML

    06-23-2009, 7:06 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Dear Friends,


    I am getting response from an Ajax call to a webservice.The response is of two types
    i.e responseText and responseXml.
    My webservice is just returning a small string as this - POINT(73.7234375 25.151953125).
    Now when I see responseText it is coming something very huge with HTML tags.

    Now when I see responseXML it is coming like this:

     <?xml version="1.0" encoding="utf-8" ?>
      <string xmlns="http://tempuri.org/">POINT(73.7234375 25.151953125)</string>

    But when I try to parse the ROOT Node and get its value I am unable to get it - I get only errors.

    So how to get the value using responseXML and responseText.

    My code is as below:

    //  function where response from Ajax call is obtained.

    function state_Change() {
          if (req.readyState != 4) return;
          if (req.status == 200) {
           // I am getting HTTP_status 200
              //retrieve WKT and create markers
              //alert(req.status);
               //var xmldoc = req.responseXML;
              //alert(xmldoc);
              //alert(xmldoc.nodeName);
              //          alert(xmldoc.nodeName);
    //          var ptdat = xmldoc.getElementsByTagName["string"][0];
              //alert(xmldoc.getElementsByTagName["string xlmns"][0]);
              //alert(ptdat.nodeValue);
              var IE = (window.ActiveXObject && document.all) ? true : false;
              if (IE) {

                  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                  xmlDoc.async = "false";
                  //alert("part1");
                  var doc = req.responseText;  
                  alert(doc);
             }
        }
          else {
              alert("Problem retrieving XML data");
              return;
          }


      }


    I am using IE and FireFox.


    Thanks for your responses.



     

     

     

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    06-25-2009, 1:34 AM
    Answer

     Hi.

    If it just returns text, please use responseText. Please check the reference http://www.w3.org/TR/XMLHttpRequest/


    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: parse responseText or responseXML

    06-25-2009, 7:00 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Thanks for your reply. It is returning data and it can be accepted either as Text or XML.In the receiving function if i use text then i can see a lot of things in HTML including things like style, web service methods etc. So I accept the text how do parse it to get the actual data avoiding the unnecessary ones? On the other hand if i accept it as xml, a document is being formed in the receiving function but unable to parse it.
    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    06-25-2009, 7:06 AM
    Answer
  • Re: parse responseText or responseXML

    06-30-2009, 9:35 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi MAK,

    I went thru' the articles and I am trying to parse the xml in a similar way but xmlhttp.responseXML.documentElement is coming null in my case even though xmlhttp.status is 200.

    As a result I am unable to parse the data as in ParseXml function.

    How should I send the data from my webservice?

    I am using IE but also want to do the same thing in FireFox.

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    06-30-2009, 10:13 AM
    • All-Star
      59,501 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,525
    • TrustedFriends-MVPs

     Check whether the xnl structure is fine

    and you are reading the right node

  • Re: parse responseText or responseXML

    07-01-2009, 1:29 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi,

    My xml is this which is returned by a webservice:

    <?xml version="1.0" encoding="utf-8" ?>
    <string xmlns="http://tempuri.org/">
    <NewDataSet>
    <WR_district>
    <state_name>
    WEST BENGAL
    </state_name>
     <area>6751862238.46765</area>
    <dist>BARDDHAMAN</dist>
    <district_>438</district_>
     <dstrict__1>295</dstrict__1>
     <dstrict_po>454</dstrict_po>
     <indiadpo_1>36</indiadpo_1>
     <indiadpoly>309</indiadpoly>
     </WR_district>
     </NewDataSet>
    </string>

    Is it well formed or not?

    and my code is this:

    var xmlHttpObj = CreateXmlHttpRequestObject();
         
                if (xmlHttpObj != null) {

              xmlHttpObj.onreadystatechange = onResponse;
              alert('here');
              xmlHttpObj.open("GET", url, false);
              alert('here1')
              xmlHttpObj.setRequestHeader("Content-Type", "text/xml");
              //alert('here2');
              xmlHttpObj.send("arg1=" + tbname + "&arg2=" + pointWKT);
              alert('here3');
          }
          else {

              alert("Your browser does not support XMLHTTP.");
          }


    function onResponse() {
          if (xmlHttpObj.readyState != READYSTATE_COMPLETE) return;
          if (xmlHttpObj.status != 200) {
              alert(xmlHttpObj.status);
              alert("Problem retrieving XML data");
              return;
          }
          alert(xmlHttpObj.status);
          //txt = "<table border='1'>";
          x = xmlHttpObj.responseXML;
          alert(x);
          var data = x.documentElement.getElementByTagName("NewDataSet");
          alert("data:" +data);  
    //      for (i = 0; i < x.allNodes.childNode[0].length; i++)
    //      {
    //          var stateName = data.getElementByTagName("state").childNodes[0].nodeValue;
    //          var statepopulation = data.getElementByTagName("pop_tot01").childNodes[0].nodeValue;
    //          var stateMales = data.getElementByTagName("males01").childNodes[0].nodeValue;
    //          var stateFemales = data.getElementByTagName("females01").childNodes[0].nodeValue;
    //          alert(data.getElementByTagName("state"));
    //          alert(stateName + "\n" + statepopulation + "\n" + stateMales + "\n" + stateFemales);
    //      }

      }






    Now xmlHttpObj.responseXML.documentElement is null but xmlHttpObj.responseXML is an object.But can I parse xmlHttpObj.responseXML.

    Thanks for your reply.



    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    07-02-2009, 1:00 AM
    • All-Star
      59,501 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,525
    • TrustedFriends-MVPs

    sunny74:
    var data = x.documentElement.getElementByTagName("NewDataSet");
     

     

    This like has issue data is acting as an array tag NewDataSet

    hence change the above line to

    var data = x.documentElement.getElementByTagName("NewDataSet")[0];

    To get the first element

     

  • Re: parse responseText or responseXML

    07-02-2009, 1:53 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi,

    mudassarkhan:
    var data = x.documentElement.getElementByTagName("NewDataSet")[0];

    I changed it to the above but I am getting the error below:

    Microsoft JScript runtime error: 'documentElement' is null or not an object.

    Where is the problem?

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    07-02-2009, 2:25 AM
    • All-Star
      59,501 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,525
    • TrustedFriends-MVPs

     means nothing returned from the web service

  • Re: parse responseText or responseXML

    07-02-2009, 2:54 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    mudassarkhan:

     means nothing returned from the web service

    Hi,

    WEb service is returning data and therefore x is an object otherwise it would be null.

    When I self run the webservice I can see it returning the xml which I given in the in my previous post.

    It could be that the format is such that it is not able to create a xml document.

    Should I return a Xml document from the web service.

    Thanks.

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    07-02-2009, 3:02 AM
    • All-Star
      59,501 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,525
    • TrustedFriends-MVPs

     though x returns an object but something wrong hence you are unable to create xmldocument

    Also try innerHTML of x to get its content

  • Re: parse responseText or responseXML

    07-02-2009, 4:56 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi,

    x.innerHTML does not work.

    I have been trying to make this work for a week but not been able to do so till now.

    The only way I have been able to access server data from clientscript  is by using javascript proxy and web service.

    Have u tried it ur self?

    What r the other methods of server side functions from clientscript?

    Thanks for ur reply.

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
  • Re: parse responseText or responseXML

    07-02-2009, 5:22 AM
    • All-Star
      59,501 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,525
    • TrustedFriends-MVPs

     I think you should refer here it should help you

    http://www.the-art-of-web.com/javascript/ajax/

  • Re: parse responseText or responseXML

    07-03-2009, 12:56 AM
    • Participant
      1,382 point Participant
    • sunny74
    • Member since 10-20-2007, 3:47 PM
    • Posts 547

    Hi,

    The website is good but problem not solved.

    SUBHRANIL BASU RAY
    Mumbai

    TODAY'S TECHNIQUES ARE OBSOLETE FOR TOMORROW'S JOB.
    PLS MARK MY REPLY "AS ANSWER" IF IT HELPED YOU.
Page 1 of 1 (15 items)