XML ElementTree Parse.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marc.wyburn@googlemail.com

    #1

    XML ElementTree Parse.

    I'm playing with XML and elementtree and am missing something but I'm
    not sure what...? I've create an XML file with Elementtree with a root
    of backup.xml. Attached to the root is a dirob and the dirobj has a
    fileobj. fileobj has filename and filesize tags. I can open the file
    in excel and it sets out the columns as I would expect. The problem
    I'm having is parsing the file. Using..
    >>file = open('c:\\scrip ts\\backup.xml' , "r")
    >>tree = parse(file)
    >>elem = tree.getroot()
    >>elem.findtext ('filesize')
    Doesn't return anything, infact I can't seem to find anything
    regardless of what I search for.
    Here is the XML. I've got a list of files and their containing
    directories and I want my python script to append to the XML file each
    day. I could have done it with SQL but fancied banging my head on the
    wall instead. Thanks, MW.


    - <backup.xml>
    - <dirob>
    <dirname>C:\sys prep</dirname>
    - <fileob>
    <filename>test. txt</filename>
    <filesize>10</filesize>
    </fileob>
    </dirob>
    </backup.xml>

  • Fredrik Lundh

    #2
    Re: XML ElementTree Parse.

    marc.wyburn@goo glemail.com wrote:
    I'm playing with XML and elementtree and am missing something but I'm
    not sure what...? I've create an XML file with Elementtree with a root
    of backup.xml. Attached to the root is a dirob and the dirobj has a
    fileobj. fileobj has filename and filesize tags. I can open the file
    in excel and it sets out the columns as I would expect. The problem
    I'm having is parsing the file. Using..
    >
    >>>file = open('c:\\scrip ts\\backup.xml' , "r")
    >>>tree = parse(file)
    >>>elem = tree.getroot()
    >>>elem.findtex t('filesize')
    >
    Doesn't return anything, infact I can't seem to find anything
    regardless of what I search for.
    find/findtext/findall only look at direct subelements, unless you use xpath-
    style syntax. in this case,

    elem.findtext(" dirob/fileob/filesize")

    should do the trick. or you could do something like

    for dir_elem in tree.findall("d irob"):
    for file_elem in dirob.findall(" fileob"):
    print file_elem.findt ext("filesize")

    to loop over all directory objects at the top level, and print the sizes for
    all files in those directories.

    to get the first filesize in the tree, no matter where it is, use

    elem.findtext(" .//filesize")

    </F>
    - <backup.xml>
    - <dirob>
    <dirname>C:\sys prep</dirname>
    - <fileob>
    <filename>test. txt</filename>
    <filesize>10</filesize>
    </fileob>
    </dirob>
    </backup.xml>


    Comment

    • marc.wyburn@googlemail.com

      #3
      Re: XML ElementTree Parse.

      Thanks Fredrik, thats got me started but just incase anyone looks there
      is a slight mistype in your code...

      or you could do something like
      >
      for dir_elem in tree.findall("d irob"):
      for file_elem in dirob.findall(" fileob"):
      print file_elem.findt ext("filesize")
      >
      to loop over all directory objects at the top level, and print the sizes for
      all files in those directories.
      should read
      >>for delem in tree.findall("d irob"):
      .... for felem in delem.findall(" fileob"):
      .... print felem.findtext( "filesize")
      ....
      933888
      9365

      Comment

      Working...