尝试从XML文件中计算DOM子元素的数量

时间:2016-10-30 23:37:06

标签: javascript xml dom

我正在尝试编写一些简单的代码来显示2个XML文件的直接子元素的数量。这是迄今为止我提出的最佳代码。

<script>
    var xmlDoc = loadXMLDoc("W6 XML and JS.xml");       
    x = xmlDoc.documentElement.childNodes;
    for (i = 0; i < x.length; i++) {

    }
    document.write("There are " + i + " immediate child elements directly under the root element.<br />");
</script>

因此,对于这些的XML输入,我有这个。我试图让它显示2个子元素而不是所有元素。

&#13;
&#13;
<dataroot>
  <Courses>
    <Department>CIS</Department>
    <CourseNumber>141</CourseNumber>
    <Title>intro to access</Title>
    <Description>db stuff</Description>
    <Credits>5</Credits>
    <Prerequisites>108, 123</Prerequisites>
    <DateAdded>2011-07-01T00:00:00</DateAdded>
  </Courses>
  <Courses>
    <Department>cap</Department>
    <CourseNumber>102</CourseNumber>
    <Title>Nutrition for the Food Service Professional</Title>
    <Description>Nutrition for the Food Service Professional</Description>
    <Credits>5</Credits>
    <DateAdded>2001-01-01T00:00:00</DateAdded>
  </Courses>
</dataroot>
&#13;
&#13;
&#13;

我知道有一个简单的答案,我现在无法找到它。任何帮助表示赞赏(这个noob谢谢你)

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
function getElementCount() {
  var doc = `<Course>
  <Department>CIS</Department>
  <CourseNumber>141</CourseNumber>
  <Title>intro to access</Title>
  <Description>db stuff</Description>
  <Credits>5</Credits>
  <Prerequisites>108, 123</Prerequisites>
  <DateAdded>2011-07-01T00:00:00</DateAdded>
</Course>`;
  var parser = new DOMParser(),
    xmlDoc = parser.parseFromString(doc, "text/xml"),
    elementCount = xmlDoc.documentElement.childElementCount;

  console.log(`There are ${elementCount} immediate child elements directly under the root element.`);
}
getElementCount();
&#13;
&#13;
&#13;

如果您只想打印文件中的子元素数量,您只需要检查代码段中提到的ChildElementCount。

&#13;
&#13;
function getElementCount() {
  var doc = `<dataroot>
  <Courses>
    <Department>CIS</Department>
    <CourseNumber>141</CourseNumber>
    <Title>intro to access</Title>
    <Description>db stuff</Description>
    <Credits>5</Credits>
    <Prerequisites>108, 123</Prerequisites>
    <DateAdded>2011-07-01T00:00:00</DateAdded>
  </Courses>
  <Courses>
    <Department>cap</Department>
    <CourseNumber>102</CourseNumber>
    <Title>Nutrition for the Food Service Professional</Title>
    <Description>Nutrition for the Food Service Professional</Description>
    <Credits>5</Credits>
    <DateAdded>2001-01-01T00:00:00</DateAdded>
  </Courses>
</dataroot>`;
  var parser = new DOMParser(),
    xmlDoc = parser.parseFromString(doc, "text/xml"),
    elementCount = xmlDoc.documentElement.childElementCount;

  console.log(`There are ${elementCount} immediate child elements directly under the root element.`);
}
getElementCount();
&#13;
&#13;
&#13;

您可以在代码中执行此操作

&#13;
&#13;
var xmlDoc = loadXMLDoc("W6 XML and JS.xml");
x = xmlDoc.documentElement.childElementCount;

document.write("There are " + x + " immediate child elements directly under the root element.<br />");
&#13;
&#13;
&#13;