读取xml子节点jquery上的多个xml属性

reading multiple xml attributes on xml child nodes jquery

本文关键字:xml 属性 子节点 jquery 读取      更新时间:2023-09-26

大家好,我正试图使用jquery解析一个xml文档,但我一直遇到限制,我想是这样。当一个节点有多个子节点时,我只得到第一个节点。让我举一个例子。

  <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>

我使用以下代码试图解析这些数据,但它只得到了第一个属性,我不知道我做错了什么。这是代码。

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

我最终应该得到A1和B1,但我只得到了A1。任何想法建议我将永远伟大

attr方法将只从匹配的元素集中获取第一个元素的属性值。如果您希望所有元素都使用each或其他循环,并从每个元素中获取属性值。试试这个。

  $(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.
  });

否,.attr()

描述:获取匹配元素集中第一个元素的属性值。

您匹配了2个<c>,但.attr()返回了它应该返回的值。