0

I am trying to parse an XML using DOM parser. I would like to get the count of the records in the XML basing on the type "Senior Software Developer". Below is my XML file. Could someone suggest me how do I get the count.

<class>
   <employee type="Senior Software Developer">
      <empid>A433568</empid>
      <empname>John Mathews</empname>
      <address>6th Avenue Street</address>
   </employee>
  <employee type="Junior Software Developer">
      <empid>A433678</empid>
      <empname>Sunny Mathews</empname>
      <address>5th Avenue Street</address>
   </employee>
  <employee type="Trainee">
     <empid>A434567</empid>
      <empname>Brad Hodge</empname>
      <address>4th Avenue Street</address>
   </employee>
  <employee type="Senior Software Developer">
      <empid>A433599</empid>
      <empname>Glenn Powell</empname>
      <address>6th Avenue Street</address>
   </employee>
   <employee type="Senior Software Developer">
      <empid>A433588</empid>
      <empname>Recordo Mathews</empname>
      <address>6th Avenue Street</address>
   </employee>
</class>
Sawyer
  • 171
  • 2
  • 12
  • 1
    And where is your code? – f1sh Mar 16 '16 at 08:50
  • First start writing code – Jens Mar 16 '16 at 08:50
  • Possible duplicate of [How to get specific XML elements with specific attribute value?](http://stackoverflow.com/questions/8445408/how-to-get-specific-xml-elements-with-specific-attribute-value) – Arnaud Mar 16 '16 at 08:50
  • I am unable to get it to do through DOM. I am trying with Storing the type in arraylist and then getting the count of the duplicates. Is this the right way to do? – Sawyer Mar 16 '16 at 09:10

1 Answers1

1

Please refer to a similar question How to get specific XML elements with specific attribute value?

The following python script can also be used to get the details.

import xml.dom.minidom
import collections

xml_data = xml.dom.minidom.parse("test.xml")

emps = xml_data.getElementsByTagName("employee")

ssd = [emp.getAttribute('type') for emp in emps]

print collections.Counter(ssd)
Community
  • 1
  • 1
Thiru
  • 3,293
  • 7
  • 35
  • 52