SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with extracting values from xml

    Hi there again all
    I am having trouble with retrieving data from an xml file
    Here is the code I am using can anyone tell me where my problem lies?
    Code:
    function GetxmlHttpObject()
    {
    var xmlHttp=null;
    try
    {
    // Firefox, Opera, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp;
    }
      
    function GetxmlHttpObject() {
      var xmlDoc = GetxmlHttpObject();
      xmlDoc.load('coords.xml');
      var len = xmlDoc.childNodes.length;
     var newcoords = xmlDoc.childNodes(0).childNodes(0).firstChild.text;
     	parent.document.receiver.thepos.value = len;  
    }
    as I am very new to Javascript and AJAX I am not very familiar with the process yet and I have not been able to get the xml loaded.
    Obviously I am declaring this all wrong as I receive the error that load is not a function of xmlDoc!

  2. #2
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there again I have changed tactics and am now using pototype.js
    I seem to be able to get the tags though I am not sure how to access the text values of each tag.
    can anyone help?
    Code:
    function makeMarkerXml() {
    new Ajax.Request('coords.xml',
      {
        method:'post',
        onSuccess: function(transport){
    var coordTags = transport.responseXML.getElementsByTagName('coords');
    var coord =coordTags[0].text;///need to find the text and this doesn't work
    parent.document.receiver.thecoord.value = 'XML child:'+coord;
         
        },
        onFailure: function(){ alert('Call failed') }
      });
    
     
    }

  3. #3
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    656
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by firehawk777 View Post
    Hi there again I have changed tactics and am now using pototype.js
    I seem to be able to get the tags though I am not sure how to access the text values of each tag.
    can anyone help?
    Code:
    function makeMarkerXml() {
    new Ajax.Request('coords.xml',
      {
        method:'post',
        onSuccess: function(transport){
    var coordTags = transport.responseXML.getElementsByTagName('coords');
    var coord =coordTags[0].text;///need to find the text and this doesn't work
    parent.document.receiver.thecoord.value = 'XML child:'+coord;
         
        },
        onFailure: function(){ alert('Call failed') }
      });
    
     
    }
    Code JavaScript:
    var coord =coordTags[0].innerText;

  4. #4
    SitePoint Enthusiast firehawk777's Avatar
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Thanks though this didn't work.
    I can see the xml is loaded though firebug though I just can't seem to work out why I can't get the text value of each tag. I know theres nothing wrong with the xml as I can load and iterate the values in both flash AS and php.
    anyone know how to retrieve the text value of a child node in xml.
    Here is the xml format

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <cat>
    <coord>
    <coords>(-27.982240960539567, 153.4050145507813)</coords>
    <description>A PHAT GIG</description>
    <gid>61</gid>
    <num>2</num>
    <image>g_marker2.png</image>
    <type>Gig</type>
    </coord>
    <coord>
    <coords>(-28.00224937595136, 153.4269872070313)</coords>
    <description>A Big Festival</description>
    <gid>63</gid>
    <num>3</num>
    <image>r_marker3.png</image>
    <type>Festival</type>
    </coord>
    <coord>
    </cat>

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your XML has a mismatched <coord> tag at the end. Fix it and this will alert the data:
    Code:
    <html>
     <head>
      <title>TEST</title>
      
      
    <script type="text/javascript">
       
    
    function createRequest() 
    {
     var request = null;  
     
     try {
        request = new XMLHttpRequest();
      } catch( e ) {
        try {
          request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch ( ee ) {
          try {
            request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch ( eee ) {
            request = null;
          }
        }
      }
    
     return request;
    }
    
    rq = createRequest();
    
    rq.onreadystatechange = function()
    {
     var XMLDoc;
       
     if( this.readyState == 4 )
     {
      if( this.status == 200 )
      {
       alert( this.responseXML.getElementsByTagName( 'coords' )[0].firstChild.nodeValue );
      }
      else
       alert('ERROR: ' + this.status + '\n\n' + this.statusText ) ;  
     } 
    }
    
    rq.open( 'get', 'coords.xml?r='+new Date().getTime(), true );
    
    rq.send( null );
    
    
    </script>
    </head>
    <body>
    </body>
    </html>
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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