如何在jquery中用重复的子句解析xml?

时间:2016-06-16 10:36:49

标签: jquery xml xml-parsing

根据下面的示例xml,我们需要以相同的顺序解析文本。我使用find()方法获取节点的值,但所有段落组合在一起,所有多媒体组合在一起。

如何获得如下所示的预期输出 任何帮助将不胜感激       

        <section ID="warn">
           <title>WARNINGS</title>
           <text>
              <paragraph>first sample text</paragraph>
              <multimedia>sample Media1</multimedia>
              <paragraph>Second sample text</paragraph>
              <paragraph>Third sample text</paragraph>
              <paragraph>Fourth sample text</paragraph>
              <multimedia>sample Media2</multimedia>
              <paragraph>Fifth sample text</paragraph>
           </text>

     </component>

Expected output should be like below

WARNINGS
first sample text
sample Media1
Second sample text
Third sample text
Fourth sample text
sample Media2

1 个答案:

答案 0 :(得分:0)

你可以使用,

var nodes = $("section *").filter(function() {
  return $(this).children().length == 0;
});

nodes.each(function() {
  console.log($(this).text());
});

Fiddle

  • 过滤掉没有子元素的结束节点。
  • 然后循环浏览它们以获取文本。