Brian Thompson Brian Thompson - 1 month ago 5
Javascript Question

Trying to Count Number of DOM child elements from an XML file

I am trying to write some simple code to display the number of immediate child elements there are for 2 XML files. Here is the best code I have come up with so far.

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


So for XML input on these I have this. I am trying to get it to show 2 child elements and not all elements.



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





I know there is a simple answer I am just not able to find it right now. any help is appreciated ( This noob thanks you)

Answer

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();

If you are looking to just print the documents immediate child element count, you just need to check on the ChildElementCount as mentioned in the snippet.

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();

You could do this in your code

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 />");

Comments