【问题标题】:Jquery - How to split and get the actual data from an XML file.?Jquery - 如何从 XML 文件中拆分和获取实际数据。?
【发布时间】:2013-06-13 13:58:42
【问题描述】:

想要从存在多条记录的 XML 中拆分并获取实际数据。

XML

<description>
    <div><b>Article_Title:</b> Title_Content</div>
    <div><b>Article_Summary:</b> Summary_Content</div>
    <div><b>Article_Date:</b> 05/08/2013</div>
</description>

从上面的代码中,我只想检索“Title_Content”、“Summary_Content”和“05/08/2013”​​。

获取这些内容的最佳方式是什么。

提前致谢

【问题讨论】:

    标签: javascript jquery xml xml-parsing


    【解决方案1】:

    看看这个函数jQuery.parseXML

    【讨论】:

    • 这只会创建一个有效的 XML 文档。哪个操作已经有了。
    【解决方案2】:

    假设它被解析为有效的 XML

    var values = [];
    
    $('description div').clone().find('b').remove().end().each(function(ix){
       values[ix] = $(this).text();   
    });
    

    【讨论】:

      【解决方案3】:

      您可以执行以下操作

      HTML

      <description>
          <div><b>Article_Title:</b> Title_Content</div>
          <div><b>Article_Summary:</b> Summary_Content</div>
          <div><b>Article_Date:</b> 05/08/2013</div>
      </description>
      

      Javascript

      var descriptions = document.getElementsByTagName("description"),
          results = [];
      
      Array.prototype.forEach.call(descriptions, function (description) {
          var result = {};
      
          result.title = description.children[0].lastChild.textContent;
          result.summary = description.children[1].lastChild.textContent;
          result.date = description.children[2].lastChild.textContent;
      
          results.push(result);
      });
      
      console.log(results);
      

      输出

      date: " 05/08/2013"
      summary: " Summary_Content"
      title: " Title_Content"
      

      开启jsfiddle

      如果您不想要前导空格或尾随空格,请将.trim() 添加到每个textContent 的末尾

      forEach 循环仅在您定义了多个此类块时才需要。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        • 2011-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多