Parsing XML with Javascript - Newbie Need Help!!

I have the following XML, and I need to parse the information in <z:row to an array in javascript.  I am not sure how to do this or get started.  I need 2 arrays (region,tier) & (region,health).

- <xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
- <s:Schema id="RowsetSchema">
- <s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30">
- <s:AttributeType name="ows_ProjCategory" rs:name="Tier Level" rs:number="1">
  <s:datatype dt:type="string" dt:maxLength="512" />
  </s:AttributeType>
- <s:AttributeType name="ows_Region" rs:name="Region" rs:number="2">
  <s:datatype dt:type="string" dt:maxLength="512" />
  </s:AttributeType>
- <s:AttributeType name="ows_Overall_x0020_Project_x0020_Heal0" rs:name="Overall Project Health" rs:number="3">
  <s:datatype dt:type="variant" dt:lookup="true" dt:maxLength="8009" />
  </s:AttributeType>
  </s:ElementType>
  </s:Schema>
- <rs:data>
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#02 AMBER" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 3" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 3" ows_Region="Region1" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#02 AMBER" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 3" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 3" ows_Region="Region2" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#02 AMBER" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 1" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 2" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 3" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  <z:row ows_ProjCategory="Tier 3" ows_Region="Region3" ows_Overall_x0020_Project_x0020_Heal0="string;#03 GREEN" />
  </rs:data>
  </xml>
LVL 5
tobey1Asked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
alert("Got this far");
var xml = xmlHttp.responseXML;
var allRows = xml.getElementsByTagName('z:row');
0
 
Michel PlungjanIT ExpertCommented:
var allRows = xml.getElementsByTagName('z:row');

should do it
0
 
tobey1Author Commented:
so just load the XML document and use that line of code...I am really new at this so excuse teh dumb questions.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Michel PlungjanIT ExpertCommented:
Yes - using xmlhttp for example
0
 
tobey1Author Commented:
So here is my code...doesn't seem to be working...can you advise?

    function Arraybuilder()
    {
alert('Arraybuilder() Script Started');
      var url = "http://my-site/xmldoc.xml";
alert("url="+url);      
      var xmlhttp=new XMLHttpRequest();
alert("xmlhttp="+xmlhttp);      
      xmlhttp.open("GET",url,true);
      var allRows = xmlhttp.getElementsByTagName('z:row');
alert("allRows="+allRows);      
    }
0
 
Michel PlungjanIT ExpertCommented:
That is not a correct call
You need to wait until the file is loaded

Have a look at w3schools for the correct code, I have to go to bed right now
0
 
tobey1Author Commented:
I will be looking at this today.  Thanks.
0
 
tobey1Author Commented:
I think I am getting closer.  I now get the readystate of 4 (loaded), and I can display in an alert the responseText, still unable to parse the XML or the text.

<script language="javascript" type="text/javascript">
                                                      alert('Starting Script');
                                                    _spBodyOnLoadFunctionNames.push("Arraybuilder");

                                                    function Arraybuilder()
                                                    {
                                                            alert('Arraybuilder() Script Started');
                                                            var url = "http://my-site/doc.xml";
                                                            alert("url="+url);      
                                                            var xmlhttp
                                                            try
                                                            {
                                                                   // Firefox, Opera 8.0+, Safari
                                                              xmlHttp = new XMLHttpRequest();
                                                          }
                                                          catch (e)
                                                          {
                                                              try
                                                              {
                                                                        // Internet Explorer
                                                                  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                                                              }
                                                              catch (e)
                                                              {
                                                                  try
                                                                  {
                                                                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                                                                  }
                                                                  catch (e)
                                                                  {
                                                                      alert("Your browser does not support AJAX!");
                                                                      return false;
                                                                  }
                                                              }
                                                          }
                                                          // Event Handler - EventListener
                                                          xmlHttp.onreadystatechange = function()
                                                          {
                                                                  alert("ReadyState= "+xmlHttp.ReadyState);
                                                              if (xmlHttp.readyState == 4) // 4: The Request is complete
                                                              {
                                                                    alert("Got this far");
                                                                    alert("xmlHttp.responseText= "+xmlHttp.responseText);
                                                                    var allRows = xmlHttp.getElementsByTagName('z:row');
                                                                    alert("allRows= "+allRows);
                                                                  //document.getElementByTagName('z:row').innerHTML = xmlHttp.responseText; // Response
                                                              }
                                                          }
                                                          // Request
                                                          alert("Running xmlHttp.open now!");
                                                          xmlHttp.open("GET", url);
                                                          xmlHttp.send(null);
                                                            //var allRows = xmlhttp.getElementsByTagName('z:row');
                                                            //alert("allRows="+allRows);                                                                
                                                      }                                                            
                                                            
                                                      //alert("xmlhttp="+xmlhttp);      
                                                      //xmlhttp.open("GET",url);
                                                      //var allRows = xmlhttp.getElementsByTagName('z:row');
                                                      //alert("allRows="+allRows);      
                                                    //}

                                                </script>
0
 
Michel PlungjanIT ExpertCommented:
.responseXML !!!
0
 
tobey1Author Commented:
Where?  any use of .responseText is commented out.
0
 
tobey1Author Commented:
alert("Got this far");
var xmlDoc=xmlHttp.responseXML;
alert("xmlHttp.responseText= "+xmlDoc.xml);
var allRows = xmlDoc.getElementsByTagName("z:row");                                                                    
alert("allRows= "+allRows);


allRows is empty or blank in the alert.
0
 
Michel PlungjanIT ExpertCommented:
Can you try


var allRows = xmlDoc.getElementsByTagName("z");      
or
var allRows = xmlDoc.getElementsByTagName("row");      

0
 
Michel PlungjanIT ExpertCommented:
or
xmlDoc.getElementsByTagNameNS("*", "row")

0
 
Michel PlungjanIT ExpertCommented:
or more correct/less lazy

xmlDoc.getElementsByTagNameNS("z", "row")
0
 
tobey1Author Commented:
xmlDoc.getElementsByTagNameNS("z", "row") results in Error - Object doesn't support this property or method.
0
 
tobey1Author Commented:
Thanks for all your help.  I did this, and got results.

var allRows = xmlDoc.getElementsByTagName("z:row");                                                                                                                                        
                                                                    alert("allRows Length="+allRows.length);
                                                                    alert("allRows= "+allRows.item(0).attributes(1).text);
0
 
Michel PlungjanIT ExpertCommented:
So
var allRows = xmlDoc.getElementsByTagName("z:row");                                                                                                                                         
worked anyway?
0
 
tobey1Author Commented:
it did.  I wasn't referencing the allRows object correctly.  Once I looked at thie item and attribute, I was OK.  I am very new to this, but should have thought of allRows as an object vs. a text variable.
0
 
Michel PlungjanIT ExpertCommented:
Yes. That helps :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.