使用jQuery

时间:2015-09-22 17:47:05

标签: javascript jquery xml

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>
&#13;
&#13;
&#13;

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文件中提取许可证信息,但是如果你看一下“你好”这个词。这就是所有返回的东西。例如,在第一个XML条目中,我得到了#hello hello hello&#34;。

谢谢,

2 个答案:

答案 0 :(得分:1)

我还没有测试过,但看起来你需要改变这一行:

return 'hello' +  $(this).children('State').text() + " " ;

到这个

return 'hello' +  $(this).attr('State') + " " ;

答案 1 :(得分:0)

$(xml).find('employee').each(function(){
     var emp =  $(this).attr("lastName"); /* read the root */
     var images = $('licenses license',this).map(function() {
         return 'hello ' +  $(this).attr('State') + " " ;
     }).get().join('');
     console.log(images);
});

$(xml).find('employees')替换为$(xml).find('employee'),将$(this).children('State').text()替换为$(this).attr('State')