Not getting all the attributes from element tags in XML using python

I have a XML file test.xml and code I have is reading the only 1st attribute, not all the other attributes

test1.xml

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
   <product description="Cardigan Sweater" product_image="cardigan.jpg">
      <catalog_item gender="Men's">
         <item_number sep = "help" dep = "paraug" note = "zempu">QWZ5671</item_number>
         <line cap = "delp" des = "laraug" fote = "cat">QWZ5671</line>
         <cool_number>QWZ5671</cool_number>
         <price>39.5</price>
      </catalog_item>
   </product>
</catalog>

code:

from lxml import etree
from collections import defaultdict

root_1 = etree.parse('test1.xml').getroot()
d1= []
for node in root_1.findall('.//catalog_item'):
    item = defaultdict(list)
    for x in node.iter():
        if x.attrib:
            item[x.attrib.keys()[0]].append(x.attrib.values()[0])
        if x.text.strip():
            item[x.tag].append(x.text.strip())
    d1.append(dict(item))

d1 = sorted(d1, key = lambda x: x['item_number'])
print(d1)

Current output: values of 1st arributes from each element i.e. sep from <item_number> and cap from <line> are being fetched into the dictionary, NOT dep and note from <item_number> and des and fote from <line>

[{'gender': ["Men's"], 'sep': ['help'], 'item_number': ['QWZ5671'], 'cap': ['delp'], 'line': ['QWZ5671'], 'cool_number': ['QWZ5671'], 'price': ['39.5']}]

Expected output: to fetch all other attributes also

[{'gender': ["Men's"], 'sep': ['help'], 'dep': ['paraug'], 'note': ['zempu'],'item_number': ['QWZ5671'], 'cap': ['delp'], 'des': ['laraug'], 'fote': ['cat'], 'line': ['QWZ5671'], 'cool_number': ['QWZ5671'], 'price': ['39.5']}]

Upvotes: 0

Views: 71

Answers (1)

sushanth
sushanth

Reputation: 8302

try this,

for node in root_1.findall('.//catalog_item'):
    item = defaultdict(list)
    for x in node.iter():
        # iterate over the items
        for k, v in x.attrib.items():
            item[k].append(v)

        if x.text.strip():
            item[x.tag].append(x.text.strip())

    d1.append(dict(item))

Upvotes: 2

Related Questions