G
G
gadzhi152016-03-31 00:07:39
Python
gadzhi15, 2016-03-31 00:07:39

How to parse xml file in python?

There is an XML file. I'll give you a piece of it:

<eventsList>
<event>
        <id>374648e7-0c33-403f-bec5-c580fcf9fc8b</id>
        <date>2016-03-30T21:11:33.163+03:00</date>
        <type>deletedPrintedItems</type>
        <departmentId>3</departmentId>
        <attribute>
            <name>comment</name>
            <value>sadasd</value>
        </attribute>
        <attribute>
            <name>user</name>
            <value>65d90cba-a421-4e4d-9d3e-14ddd6907280</value>
        </attribute>
        <attribute>
            <name>openTime</name>
            <value>Wed Mar 30 21:07:53 MSK 2016</value>
        </attribute>
        <attribute>
            <name>tableNum</name>
            <value>2</value>
        </attribute>
        <attribute>
            <name>session</name>
            <value>1.000000000</value>
        </attribute>
        <attribute>
            <name>orderNum</name>
            <value>4.000000000</value>
        </attribute>
        <attribute>
            <name>reason</name>
            <value>Со списанием</value>
        </attribute>
        <attribute>
            <name>receiptsSum</name>
            <value>0E-9</value>
        </attribute>
        <attribute>
            <name>dishes</name>
            <value>Салат Оливье</value>
        </attribute>
        <attribute>
            <name>isBanquet</name>
            <value>0E-9</value>
        </attribute>
        <attribute>
            <name>withWriteoff</name>
            <value>1.000000000</value>
        </attribute>
        <attribute>
            <name>penalty</name>
            <value>0E-9</value>
        </attribute>
        <attribute>
            <name>rowCount</name>
            <value>1.000000000</value>
        </attribute>
        <attribute>
            <name>numGuests</name>
            <value>4.000000000</value>
        </attribute>
        <attribute>
            <name>auth</name>
            <value>65d90cba-a421-4e4d-9d3e-14ddd6907280</value>
        </attribute>
        <attribute>
            <name>orderSumAfterDiscount</name>
            <value>0E-9</value>
        </attribute>
        <attribute>
            <name>terminal</name>
            <value>49c7d0ba-a469-517f-0153-61a7bcb29ccb</value>
        </attribute>
        <attribute>
            <name>sum</name>
            <value>70.000000000</value>
        </attribute>
        <attribute>
            <name>method</name>
            <value>Списание за счет заведения</value>
        </attribute>
        <attribute>
            <name>orderId</name>
            <value>e55c392f-ea8a-4c96-aecd-2bd0b6ffd2cd</value>
        </attribute>
        <attribute>
            <name>waiter</name>
            <value>5daf4283-ae8c-4a95-83e5-052fb4a33570</value>
        </attribute>
    </event>
    <event>
        <id>61d3cb56-74dd-4d20-86ae-217c37f15f01</id>
        <date>2016-03-30T21:11:41.090+03:00</date>
        <type>frontLogout</type>
        <departmentId>3</departmentId>
        <attribute>
            <name>user</name>
            <value>65d90cba-a421-4e4d-9d3e-14ddd6907280</value>
        </attribute>
        <attribute>
            <name>session</name>
            <value>1.000000000</value>
        </attribute>
        <attribute>
            <name>terminal</name>
            <value>49c7d0ba-a469-517f-0153-61a7bcb29ccb</value>
        </attribute>
    </event>
</eventsList>

I need to find the deletedPrintedItems event in the type tag. If it is, then you need to display the contents of some attributes as well (for example, where attribute / name (comment) contains the value value)
Wrote code in Python
from lxml import etree
tree = etree.parse('xml_iiko.xml')
nodes = tree.xpath('/eventsList/event/type')
for node in nodes:
    if node.text == 'deletedPrintedItems':

but then I don't know how to implement all that is needed. Who can push or show in the documentation where to go?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abcd0x00, 2016-03-31
@gadzhi15

Selects the desired events in the document.
In the necessary events selects the necessary attributes.
In the required attributes, selects the desired element.

The code
>>> import lxml.etree
>>> 
>>> text = """\
... <eventsList>
... <event>
...         <id>374648e7-0c33-403f-bec5-c580fcf9fc8b</id>
...         <date>2016-03-30T21:11:33.163+03:00</date>
...         <type>deletedPrintedItems</type>
...         <departmentId>3</departmentId>
...         <attribute>
...             <name>comment</name>
...             <value>sadasd</value>
...         </attribute>
...         <attribute>
...             <name>user</name>
...             <value>65d90cba-a421-4e4d-9d3e-14ddd6907280</value>
...         </attribute>
...         <attribute>
...             <name>openTime</name>
...             <value>Wed Mar 30 21:07:53 MSK 2016</value>
...         </attribute>
...         <attribute>
...             <name>tableNum</name>
...             <value>2</value>
...         </attribute>
...         <attribute>
...             <name>session</name>
...             <value>1.000000000</value>
...         </attribute>
...         <attribute>
...             <name>orderNum</name>
...             <value>4.000000000</value>
...         </attribute>
...         <attribute>
...             <name>reason</name>
...             <value>Со списанием</value>
...         </attribute>
...         <attribute>
...             <name>receiptsSum</name>
...             <value>0E-9</value>
...         </attribute>
...         <attribute>
...             <name>dishes</name>
...             <value>Салат Оливье</value>
...         </attribute>
...         <attribute>
...             <name>isBanquet</name>
...             <value>0E-9</value>
...         </attribute>
...         <attribute>
...             <name>withWriteoff</name>
...             <value>1.000000000</value>
...         </attribute>
...         <attribute>
...             <name>penalty</name>
...             <value>0E-9</value>
...         </attribute>
...         <attribute>
...             <name>rowCount</name>
...             <value>1.000000000</value>
...         </attribute>
...         <attribute>
...             <name>numGuests</name>
...             <value>4.000000000</value>
...         </attribute>
...         <attribute>
...             <name>auth</name>
...             <value>65d90cba-a421-4e4d-9d3e-14ddd6907280</value>
...         </attribute>
...         <attribute>
...             <name>orderSumAfterDiscount</name>
...             <value>0E-9</value>
...         </attribute>
...         <attribute>
...             <name>terminal</name>
...             <value>49c7d0ba-a469-517f-0153-61a7bcb29ccb</value>
...         </attribute>
...         <attribute>
...             <name>sum</name>
...             <value>70.000000000</value>
...         </attribute>
...         <attribute>
...             <name>method</name>
...             <value>Списание за счет заведения</value>
...         </attribute>
...         <attribute>
...             <name>orderId</name>
...             <value>e55c392f-ea8a-4c96-aecd-2bd0b6ffd2cd</value>
...         </attribute>
...         <attribute>
...             <name>waiter</name>
...             <value>5daf4283-ae8c-4a95-83e5-052fb4a33570</value>
...         </attribute>
...     </event>
...     <event>
...         <id>61d3cb56-74dd-4d20-86ae-217c37f15f01</id>
...         <date>2016-03-30T21:11:41.090+03:00</date>
...         <type>frontLogout</type>
...         <departmentId>3</departmentId>
...         <attribute>
...             <name>user</name>
...             <value>65d90cba-a421-4e4d-9d3e-14ddd6907280</value>
...         </attribute>
...         <attribute>
...             <name>session</name>
...             <value>1.000000000</value>
...         </attribute>
...         <attribute>
...             <name>terminal</name>
...             <value>49c7d0ba-a469-517f-0153-61a7bcb29ccb</value>
...         </attribute>
...     </event>
... </eventsList>
... """
>>> 
>>> doc = lxml.etree.fromstring(text)
>>> events = doc.xpath(r'//event/type[text() = "deletedPrintedItems"]/..')
>>> 
>>> for event in events:
...     comments = event.xpath(r'.//attribute/name[text() = "comment"]'
...                            r'/../value/text()')
...     print(comments)
... 
['sadasd']
>>>

I
iegor, 2016-03-31
@iegor

Iterate over the event, in each find the type, if it suits you, in this event extract the required attribute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question