在javascript中获取xml dom子元素

时间:2015-01-21 20:16:22

标签: javascript xml

我有一个这样的XML文档:

<section>
   <description></description>
   <question>
       <description></description>
   </question>
</section>

我将sectionXML存储在一个名为SectionXML的变量中。我用:

section.description = SectionXML.getElementsByTagName("description")[0].childNodes[0];

section.description = section.description ? section.description.nodeValue : "";

然而,SectionXML.getElementsByTagName("description")返回许多描述的列表,好像我们看到更多的孩子(伟大的孩子等),我们可以看到他们都有描述,但我只想得到SectionXML的直接孩子标签名称为&#34; description&#34;。我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用querySelectorAll():

SectionXML.querySelectorAll("section>description")[0]
// use > to get direct element only