简体   繁体   中英

Javascript to parse XML file

I am a bit novice in JS and trying to parse and XML file as a response via an AJAX call which is following format:

<chart>
     <categories>
        <name>'MSIE'</name>
        <name>'Firefox'</name>
        <name>'Chrome'</name>
        <name>'Safari'</name>
        <name>'Opera'</name>
      </categories>
      <name>'Browser Brands'</name>
      <data>
             <series>
               <y>55.11</y>
               <drilldown>
                  <name>'MSIE versions'</name>
                  <categories>
                      <name>'MSIE 8.0'</name>
                      <name>'MSIE 6.0'</name>
                      <name>'MSIE 7.0'</name>
                      <name>'MSIE 9.0'</name>
                  </categories>
                 <data>
                     <series>
                       <y>33.06</y>
                       <drilldown>
                           <name>'drilldown next level'</name>
                            <categories>
                              <name>'a'</name>
                              <name>'b'</name>
                              <name>'c'</name>
                            </categories>
                            <data>
                              <point>23</point>
                              <point>54</point>
                              <point>47</point>
                             </data>
                       </drilldown>
                     </series>
                   <point>10.85</point>
                   <point>7.35</point>
                   <point>2.41</point>
                 </data>
               </drilldown>
             </series>
             <series>
               <y>21.63</y>
                <drilldown>
                <name>'Firefox versions'</name>
                <categories>
                    <name>'Firefox 3.6'</name>
                    <name>'Firefox 4.0'</name>
                    <name>'Firefox 3.5'</name>
                    <name>'Firefox 3.0'</name>
                    <name>'Firefox 2.0'</name>
                </categories>
                 <data>
                    <point>13.52</point>
                    <point>5.43</point>
                    <point>1.58</point>
                    <point>0.83</point>
                    <point>0.20</point>
                 </data>
               </drilldown>
             </series>
             <series>
               <y>11.94</y>
               <drilldown>
                <name>'Chrome versions'</name>
                <categories>
                   <name>'Chrome 10.0'</name>
                   <name>'Chrome 11.0'</name>
                   <name>'Chrome 8.0'</name>
                   <name>'Chrome 9.0'</name>
                   <name>'Chrome 12.0'</name>
                   <name>'Chrome 6.0'</name>
                   <name>'Chrome 5.0'</name>
                   <name>'Chrome 7.0'</name>
                </categories>
                <data>
                   <point>9.91</point>
                   <point>0.50</point>
                   <point>0.36</point>
                   <point>0.32</point>
                   <point>0.22</point>
                   <point>0.19</point>
                   <point>0.12</point>
                   <point>0.12</point>
               </data>
               </drilldown>
               </series>
               <series>
               <y>7.15</y>
               <drilldown>
               <name>'Safari versions'</name>
               <categories>
                   <name>'Safari 5.0'</name>
                   <name>'Safari 4.0'</name>
                   <name>'Safari Win 5.0'</name>
                   <name>'Safari 4.1'</name>
                   <name>'Safari/Maxthon'</name>
                   <name>'Safari 3.1'</name>
                   <name>'Safari 41'</name>
               </categories>
               <data>
                   <point>4.55</point>
                   <point>1.42</point>
                   <point>0.23</point>
                   <point>0.21</point>
                   <point>0.20</point>
                   <point>0.19</point>
                   <point>0.14</point>
               </data>
            </drilldown>
           </series>
           <series>
              <y>2.14</y>
               <drilldown>
               <name>'Opera versions'</name>
               <categories>
                  <name>'Opera 11.x'</name>
                  <name>'Opera 10.x'</name>
                  <name>'Opera 9.x'</name>
               </categories>
                <data>
                   <point>1.65</point>
                   <point>0.37</point>
                   <point>0.12</point>
                 </data>
                </drilldown>
            </series>
        </data>
</chart>

So the repeating and traversing nodes I believe should be in following way:

series->drilldown->series->drilldown....
drilldown->categories
drilldown->data

Traversing path approach to be taken =
loop
{ 
          for (mainseries)
          {
              get y
              for(series->drilldown)
              {
                 get name, categories(names), data(points)
                 for(drilldown->series)
                 {
                        mainseries = currentseries
                 }
              }
         }
} 

As I am a novice I am somehow not getting the traversing and parsing logic which I want to implement:

  $.get('ex.xml', function(xml) {
                var $xml = $(xml);
                $xml.find('chart categories name').each(function(i, category) {
                        options.xAxis.categories.push($(category).text());
                });
                chart.setTitle.push($xml.find('chart name').text());
                $xml.find('series').each(function(i, series) {
                                var seriesOptions = {
                                        y: parseInt($(series).find('y').text()),
                                        drilldown : []
                                        };
                                        $xml.find('series drilldown').each(function(i, drilldown) {
                                                name : $(drilldown).find('name').text()
                                                categories : [],
                                                data: []
                                                $xml.find('drilldown categories').each(function(i, categories) {
                                                        name : $(categories).find('name').text()
                                                        });
                                                $xml.find('drilldown data').each(function(i, data) {
                                                        $xml.find('data point').each(function(i, point) {
                                                                seriesOptions.drilldown.data.push(parseInt($(point).text()))
                                                        });
                                                });
                                        });
                });
        });

How can I have the traversing and each leaf / branch node be visited so that I can get the chart data, note that:

A series will never have values leaf node
A series is represented as a {..<property : values>..} 
A series will always one drilldown and a drilldown will have one series in itself which goes in repetition
A series will always have a proerty y or a drilldown
A drilldown will have name leaf node,categories branch and either a another series or a data branch
A category branch will always have a name leaf node
A data branch will always have values leaf node

Please help me in achieveing the objective.

Thanks in advance

I think the key to the answer you're looking for is to use the $(this) in jQuery. That is a self-referencing element within the each() function so that for each element, $(this) refers to that element.

With that in mind, you can then do:

var thisElement = $(this);
thisElement.find("some other selector").each(function(){
    var theOtherSelector = $(this);
    theOtherSelector.find("yet another selector").each(function(){
       // and so forth...
    };
})

Note that I am storing the $(this) reference to a variable so that the value is accessible within the function closure scope.

Hope this answers your question

Edit: example code for recursive depth search for your xml (haven't been checked for syntax!)

$xml.find("chart data series").each(function(){
    var chartDataSeries = $(this);
    var seriesDrillDownSearch = function(){
        var seriesDrillDownElement = $(this);

        // do your processing here ..

        seriesDrillDownElement.find("series drilldown").each(seriesDrillDownSearch);
    }();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM