B
B
blizzard2018-02-16 22:19:51
Python
blizzard, 2018-02-16 22:19:51

How to extract all attributes and their values ​​from xml and save them to a dictionary?

You need to get a dictionary, where key is an attribute, value is the value of the attributes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
longclaps, 2018-02-16
@longclaps

get it when you need it .

B
blizzard, 2018-02-17
@s41blizzard

Received, but not all. Here is the code that selects attributes, but not from a nested
tag

import xml.etree.ElementTree as ET

tree = ET.parse('/LAS.xml')
root = tree.getroot()
lst = list()
for child in root:
    for item in child.attrib:
        if item not in lst:
            lst.append(item)
print(lst)

xml itself
<?xml version="1.0" encoding="utf-8"?>
<packetinfo>
  <client syncid="18492745"  action="set"  is_carrier="f" comments="">
  </client>
  <sku syncid="18492746"  producer=""  barcode="4627109384174" need_weight="">
  </sku>
  <order syncid="18492747"  qty="1" uom="ШТ"  warehouse_id="10" shipment="МОСКВА">
  <order_detail syncid="18492748"  comments="">
    </order_detail>
  </order>
</packetinfo>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question