Here's the XML:
<employees>
<employee ID="ABC00" lastName="Smith" firstName="Bill">
<licenses>
<license State="KS" liccat="Land0003" type="Landscape Architect" includeInProposal="Y" expiration="12/31/2016"/>
<license State="MO" liccat="Land0003" type="Landscape Architect" includeInProposal="Y" expiration="12/31/2015"/>
<license State="NE" liccat="Land0003" type="Landscape Architect" includeInProposal="N" expiration=""/>
</licenses>
</employee>
<employee ID="ADM01" lastName="Smith" firstName="Sam">
<licenses>
<license> State="MO" liccat="Prof0008" type="Professional Engineer" includeInProposal="Y" expiration="12/31/2016"/>
<license State="KS" liccat="Prof0008" type="Professional Engineer" includeInProposal="Y" expiration="04/30/2017"/>
<license State="OK" liccat="Prof0008" type="Professional Engineer" includeInProposal="Y" expiration="08/31/2016"/>
</licenses>
</employee>
</employees>
Here's the code:
$.ajax({
type: "GET",
url: "NLicense.xml",
dataType: "xml",
success: function(xml) {
alert('Read the file');
$(xml).find('employees').each(function(){
var emp = $(this).attr("lastName"); /* read the root */
var images = $('licenses license',this).map(function() {
return 'hello' + $(this).children('State').text() + " " ;
}).get().join('');
console.log(images);
Here's the problem:
我正在尝试提取 XML 文件中的许可证信息,但是如果您查看单词“hello”,这就是返回的全部内容。例如,在第一个 XML 条目中,我得到“hello hello hello”。
谢谢
最佳答案
我还没有测试过,但看起来你需要更改这一行:
return 'hello' + $(this).children('State').text() + " " ;
到此
return 'hello' + $(this).attr('State') + " " ;
关于javascript - 使用 jQuery 读取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723502/