我需要遍历XML文件并选择包含子节点的特定节点(及其所有子节点),如
<ssn>11036026534</ssn>
我的目标是将数据读出为$(&#39; username&#39;,this).text(),以便我能够从所选的
中获取所有子节点<person> and down.
我尝试了多种遍历方式,但没有成功 - 最近的一次:
$(xml).each('person').find('ssn'["06106131859"]).parent( function () {
console.log('ssn match for person')
});
(这也给了我一个jquery错误)和
$(xml).find('ssn[06106131859]').parent().each( function () {...
我非常感谢能有所帮助继续前进......祝你有个美好的一天!
<person personIdHRM="10945"></person>
<person personIdHRM="11538"></person>
<person personIdHRM="1346"></person>
<person personIdHRM="23456">
<authentication>
<userId>7260132</userId>
<username>Petter Smart</username>
</authentication>
<employments>
<employment>
<employeeId>18220</employeeId>
<employmentPercentage>100.0</employmentPercentage>
<lastEmployeed>2003-11-12</lastEmployeed>
<positions>
<position isPrimaryPosition="false" validFromDate="2015-01-01">
<positionPercentage>0.0</positionPercentage>
<positionStartDate>2015-01-01</positionStartDate>
</position>
<position isPrimaryPosition="true" validFromDate="2015-01-01">
<positionPercentage>100.0</positionPercentage>
<positionStartDate>2015-01-01</positionStartDate>
</position>
</positions>
<startDate>2003-11-12</startDate>
</employment>
</employments>
<familyName>Hanson</familyName>
<genderCode>FEMALE</genderCode>
<givenName>Elizabeth</givenName>
<hrmAuthentications />
<ssn>06106131859</ssn>
</person>
答案 0 :(得分:0)
您可以使用:has()
查找包含其他元素的元素。试试这个:
var $persons = $(xml).find('person:has(ssn)');
$persons.each(function() {
var $ssn = $(this).find('ssn')
console.log($ssn.text()); // == 06106131859 in your example
});
要按person
值选择特定ssn
,请尝试以下操作:
var $person = $(xml).find('person:has(ssn:contains("06106131859"))');