Question

I have an xml file like this.

<approved-by approved="yes">
<person>
<name>XXX/name><signature>XXXXX</signature>
<location>XX</location><company>XXX</company><department>XX</department>
</person>
</approved-by>
<revision-history suppress="yes">
<rev-info>
<rev>PA1</rev>
<date><y>2013</y><m>01</m><d>22</d></date>

I need to retrieve values of 'rev' from all xmls and the value of approved. I want whtr the doc is approved or not. I have a script like this.

from xml.dom.minidom import parse, parseString
import os
import sys
def shahul(dir):   
  for r,d,f in os.walk(dir):
     for files in f:
        if files.endswith(".xml"):
            dom=parse(os.path.join(r, files))
            name = dom.getElementsByTagName('rev')
            title = dom.getElementsByTagName('title')
            approved=dom.getAttribute('approved')
            print (files, title[0].firstChild.nodeValue,name[0].firstChild.nodeValue, approved, sep='\t')
shahul("location")

I am able to get the value under 'rev' but i am not able to get value of the attribute 'approved-by'. I understand my syntax is not right to get the value of approved, but i dont know it.

i need the following as output.

FILE_NAME, Title, PA1, yes

Please guide me.

Was it helpful?

Solution

Assuming that you have only one approved-by tag in the XML:

Change:

approved = dom.getAttribute('approved')

To:

approved_by = dom.getElementsByTagName('approved-by')
approved = approved_by[0].attributes['approved'].value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top