Counting Elements in an xml file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ouray Viney

    Counting Elements in an xml file

    Hi All:

    I am looking at writing a python script that will let me parse a
    TestSuite xml file that contains n number of TestCases.

    My goal is to be able to count the <TestCaseelemen ts base on a key
    value pair in the xml node.

    Example

    <Testcase execute="true" name="foobar">

    I would like to be able to count the number of TestCases that contain
    the "execute=tr ue" but not the ones that contain "execute=false" .

    I have review the python docs and various python ebooks.

    Does anyone have any experience with this sort of thing? If so, could
    you suggest a good library and possibly some samples?

    Thanks
  • Marco Bizzarri

    #2
    Re: Counting Elements in an xml file

    On Sat, Aug 30, 2008 at 7:37 PM, Ouray Viney <oviney@gmail.c omwrote:
    Hi All:
    >
    I am looking at writing a python script that will let me parse a
    TestSuite xml file that contains n number of TestCases.
    >
    My goal is to be able to count the <TestCaseelemen ts base on a key
    value pair in the xml node.
    >
    Example
    >
    <Testcase execute="true" name="foobar">
    >
    I would like to be able to count the number of TestCases that contain
    the "execute=tr ue" but not the ones that contain "execute=false" .
    >
    I have review the python docs and various python ebooks.
    >
    Does anyone have any experience with this sort of thing? If so, could
    you suggest a good library and possibly some samples?
    Isn't the SAX part of this howto



    enough for you to create your parser?


    Regards
    Marco

    --
    Marco Bizzarri

    Where we talk about coding, Dungeons and Dragons, and stuff.

    Comment

    • Fredrik Lundh

      #3
      Re: Counting Elements in an xml file

      Ouray Viney wrote:
      I am looking at writing a python script that will let me parse a
      TestSuite xml file that contains n number of TestCases.
      >
      My goal is to be able to count the <TestCaseelemen ts base on a key
      value pair in the xml node.
      >
      Example
      >
      <Testcase execute="true" name="foobar">
      >
      I would like to be able to count the number of TestCases that contain
      the "execute=tr ue" but not the ones that contain "execute=false" .
      import xml.etree.Eleme ntTree as ET

      tree = ET.parse("filen ame.xml")

      count = 0

      for elem in tree.findall(".//Testcase"):
      if elem.get("execu te") == "true":
      count += 1

      print "found", count, "test cases"

      # tweak as necessary

      </F>

      Comment

      • Paul Boddie

        #4
        Re: Counting Elements in an xml file

        On 30 Aug, 19:37, Ouray Viney <ovi...@gmail.c omwrote:
        >
        <Testcase execute="true" name="foobar">
        >
        I would like to be able to count the number of TestCases that contain
        the "execute=tr ue" but not the ones that contain "execute=false" .
        With XPath-capable libraries, it should be enough to execute an XPath
        query on the document. For example:

        import libxml2dom
        d = libxml2dom.pars e(filename)
        number_of_cases = d.xpath("count(//Testcase[@execute='true'])")

        This applies the XPath count function to all Testcase elements in the
        document having an execute attribute with a value of 'true', thus
        returning the number of matching elements.

        Paul

        Comment

        • Ouray Viney

          #5
          Re: Counting Elements in an xml file

          On Aug 30, 2:17 pm, Paul Boddie <p...@boddie.or g.ukwrote:
          On 30 Aug, 19:37, Ouray Viney <ovi...@gmail.c omwrote:
          >
          >
          >
          <Testcase execute="true" name="foobar">
          >
          I would like to be able to count the number of TestCases that contain
          the "execute=tr ue" but not the ones that contain "execute=false" .
          >
          With XPath-capable libraries, it should be enough to execute an XPath
          query on the document. For example:
          >
            import libxml2dom
            d = libxml2dom.pars e(filename)
            number_of_cases = d.xpath("count(//Testcase[@execute='true'])")
          >
          This applies the XPath count function to all Testcase elements in the
          document having an execute attribute with a value of 'true', thus
          returning the number of matching elements.
          >
          Paul
          Hi All:

          Thank you very much for all your valuable input. All the examples
          provided are exactly what I need to get started.

          Enjoy the long weekend (for those in North America).

          Cheers

          Comment

          • Gerard flanagan

            #6
            Re: Counting Elements in an xml file

            Ouray Viney wrote:
            Hi All:
            >
            I am looking at writing a python script that will let me parse a
            TestSuite xml file that contains n number of TestCases.
            >
            My goal is to be able to count the <TestCaseelemen ts base on a key
            value pair in the xml node.
            >
            Example
            >
            <Testcase execute="true" name="foobar">
            >
            I would like to be able to count the number of TestCases that contain
            the "execute=tr ue" but not the ones that contain "execute=false" .
            >
            You might try the `count` function in the module here:



            The pseudo xpath would be something like:

            /Testcase[@execute=="true "]

            hth

            G.

            Comment

            Working...