简体   繁体   中英

reading multiple xml attributes on xml child nodes jquery

Hi guys I'm trying to parse an xml document using jquery but i keep hitting a limitation or so i think. When a node has muliple child nodes i only get the first node. let me show an example.

  <sheetData>
   <row r="1" spans="1:2" x14ac:dyDescent="0.25">
    <c r="A1" t="s">
    <v>0</v>
    </c>
     <c r="B1" t="s">
   <v>1</v>
 </c>

i use the following code to try to parse this data but it only get the first attribute i dont know what im doing wrong . here is the code.

       $(xml).find("row").each(function(i) {
        v1 =  $(this).find("c").attr("r");

i should end up with A1 and B1 but im only getting the A1. Any ideas suggestion I would be forever greatful

attr method will only get the attribute value of the first element from the matched set of elements. If you want it for all elements then use each or some other loop and get the attribute value from each element. Try this.

  $(xml).find("row").each(function(i) {
      var attrs = [];
      v1 =  $(this).find("c").each(function(){
          attrs.push($(this).attr("r"));
      });

      //Now attrs will contain both A1 and B1 attribute values.
  });

No, .attr()

Description : Get the value of an attribute for the first element in the set of matched elements.

You match 2 <c> but then .attr() returns exactly what it is supposed to.

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