N
N
nupaT2020-03-24 10:34:48
Parsing
nupaT, 2020-03-24 10:34:48

How to find value in XML?

Good afternoon!

It is necessary to display the massResults value when a value occurs in the attribute.
Parsing XML just started to learn.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<measCollecFile xmlns="Collec" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <fileHeader fileFormatVersion="1" Name="Massef">
        <fileSender elementType="MassGraph"/>
        <measCollec beginTime="2020-03-19T11:00:00+03:00"/>
    </fileHeader>
    <measData>
        <managedElement userLabel="MyLabels"/>
        <measInfo measInfoId="1">
            <granPeriod duration="2" endTime="2020-03-19T11:15:00+03:00"/>
            <repPeriod duration="1"/>
            <massTypes>1 2 3 </massTypes>
            <measValue measObjLdn="ID=19">
                <mass>63 63 0 </massResults>
            </measValue>
            <measValue measObjLdn="ID=11">
                <massResults>162 162 0 </massResults>
            </measValue>
            <measValue measObjLdn="ID=17">
                <massResults>145 145 0 </massResults>
            </measValue>
        </measInfo>
    </measData>
    <fileFooter>
        <measCollec endTime="2020-03-19T11:15:00+03:00"/>
    </fileFooter>
</measCollecFile>


Python:

from xml.dom import minidom
import re
xmldoc = minidom.parse('12.xml')
measValue = xmldoc.getElementsByTagName('measValue')
for s in measValue:
    if '11' in s.attributes['measObjLdn'].value:
        num = s+1
    print(xmldoc.getElementsByTagName('massResults')[num].firstChild.nodeValue)


(i.e. in this case it should output 162 162 0)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question