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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Michel PlungjanIT ExpertCommented:
Yes - using xmlhttp for example
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
Michel PlungjanIT ExpertCommented:
alert("Got this far");
var xml = xmlHttp.responseXML;
var allRows = xml.getElementsByTagName('z:row');
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.