开发者

Jquery XML parsing

开发者 https://www.devze.com 2023-01-25 01:23 出处:网络
I have this XML <items> <item validate=\"NotEmpty ValidEmail\" class=\"xxx yyy zzz\" type=\"input\" name=\"Id\" value=\"\" label=\"Id\" />

I have this XML

<items>
  <item validate="NotEmpty ValidEmail" class="xxx yyy zzz" type="input" name="Id" value="" label="Id" />
  <item validate="NotEmpty" type="input" name="NazovProjektu" value="" label="NazovProjektu" />
  <item type="in开发者_运维技巧put" name="Oz" value="" label="Oz" />

I want to get the value of the attribute name of each element with attribute class containing string yyy


This might work, haven't tested it yet. Assuming the XML is already parsed and inside an object named xml.

$(xml).find("[class~='yyy']").each(function(i, element) {
    console.log($(element).attr("name"));
});


I believe you can do something like

$("item[class*=yyy]", xml).each(function() { 
    $(this).attr('name'); 
});


try following, it should help you out.

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "data.xml",
    dataType: "xml",
    success: function(xml)
        {
          //find every yyy classname and alert its name
          $(xml).find(".yyy").each(function()
          {
            alert($(this).attr('name'));
          });
        }
  });
});


A class selector will get you the elements. The attr() function will get you the name of the element. Using each() on the jQuery Object will allow you to iterate through the element in the jQuery Object.

var yourXML = "...";
$("[class*='zzz']", yourXML).each(function() {
   alert($(this).attr("name"));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号