SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading XML in AJAX

    I've used Ajax to read server generated XML before, but this particular XML file is not being parsed through by any of the Ajax code I've found anywhere.

    Code:
    <?xml version="1.0"?>
    <zs:searchRetrieveResponse xmlns:zs="http://www.loc.gov/zing/srw/"><zs:version>1.1</zs:version><zs:numberOfRecords>53</zs:numberOfRecords><zs:records><zs:record><zs:recordSchema>info:srw/schema/1/dc-v1.1</zs:recordSchema><zs:recordPacking>xml</zs:recordPacking><zs:recordData><srw_dc:dc xmlns:srw_dc="info:srw/schema/1/dc-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://purl.org/dc/elements/1.1/" xsi:schemaLocation="info:srw/schema/1/dc-schema http://www.loc.gov/standards/sru/dc-schema.xsd">
      <title>The importance of being Earnest [Motion picture] </title>
      <creator>Wilde, Oscar, 1854-1900. The importance of being Earnest. [from old catalog]</creator>
      <creator>British Film Makers, ltd., London. [from old catalog]</creator>
      <creator>Universal International Films, inc. [from old catalog]</creator>
      <type>text</type>
      <publisher>[n.p.] British Film Makers, London, 1953. Released in the U. S. by Universal International Films</publisher>
      <date>[n.d.]</date>
      <language>eng</language>
    </srw_dc:dc></zs:recordData><zs:recordPosition>1</zs:recordPosition></zs:record></zs:records></zs:searchRetrieveResponse>
    The Ajax code I'm using is:

    Code:
    if(xmlHttp.readyState == 4) {
    
    var xmlResponse = xmlHttp.responseXML;
    
    xmlRoot = xmlResponse.documentElement;
    
    titleArray = xmlRoot.getElementsByTagName("title"); 
    
    alert(titleArray.item(0).firstChild.data);
    
    }
    However, I always seem to get a 'xmlRoot' in null or not an object error.

    Any suggestions? I can seem to parse other RSS feeds without any errors.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, do some debugging:
    Code:
    alert(xmlHttp.responseText);
    alert(xmlHttp.responseXml);
    If the results are as expected, it may be in the line:
    Code:
    xmlRoot = xmlResponse.documentElement;
    I've never seen it done that way. You can use my code for a light way to get element data:
    Code:
    // JavaScript Document
    var AntzXML = {
        
        init : function(xml){
        //alert('init'+xml);
            this.xml = xml;
        },
        
        getInstance : function(){
            var x = new Object();
            for(var e in AntzXML){
                x[e] = AntzXML[e];
            };
            return x;
        },
        
        getByTag : function(name, el){
            if(!el || typeof(el)=='undefined') el = this.xml;
            if(!el || typeof(el)=='undefined') return;
            return el.getElementsByTagName(name)[0].firstChild.data;
            
        },
        
        getByTagAsArray : function(name, el){
            if(!el) var els = this.xml.getElementsByTagName(name);
            else els = el.getElementsByTagName(name);
            arr = Array();
            for (var i=0;i<els.length;i++){
                arr[i]=els[i].firstChild.data;
            };
            return arr;
        },
        
        getElementsByTag : function(name){
            return this.xml.getElementsByTagName(name);
        }
        
    }
    Usage:

    Code:
    var xml = AntzXML.getInstance();
    xml.init(xmlHttp.responseXML);
    alert('Content of tag "name"'+xml.getByTag('name'));


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
  •