javascript - 使用 jQuery 读取 XML

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

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/

相关文章:

javascript - 检查 JavaScript 变量是否为 null?

javascript - 事件后退出函数

javascript - 如何停止 iframe 加载?

android - 如何将 Android XML 矢量绘图导出为另一种格式?

xml - 如何在使用 spark 解析 xml 时将标题信息添加到行信息

android - 如何从 SD 卡编辑现有 XML 文件的节点值并将其保存回来?

javascript - Vue.js 2、在css中使用自定义组件标签名?

javascript - onBackPressed 事件在 web View 中无法正常工作

javascript - JS 创建的 html 链接不会在 chrome 中打开?

javascript - croppie 插件无法使用 ajax/innerHtml 内容