невозможно получить значения из XML после etree.parsed () - PullRequest
0 голосов
/ 17 апреля 2020

Итак, я проанализировал xml файл с lxml.

import from lxml import etree
In [5]: root = etree.parse(fileXml)
In [6]: root
Out[6]: <lxml.etree._ElementTree at 0x7f2fa63ae388>

Как видите, объект содержит 115139 записей ... или, по моему мнению, это мое понимание ...

In [21]: len(root.getroot())
Out[21]: 115139

Если я получу первое, я увижу некоторые поля, которые я ожидаю от него:

In [11]: root.getroot()[0].getchildren()
Out[11]: 
[<Element {xmlapi_1.0}receivedOctetsPeriodic at 0x7f2fa815df88>,
<Element {xmlapi_1.0}transmittedOctetsPeriodic at 0x7f2fa5b99508>,
<Element {xmlapi_1.0}inputSpeed at 0x7f2fa5b994c8>,
<Element {xmlapi_1.0}outputSpeed at 0x7f2fa5b99408>,
<Element {xmlapi_1.0}timeCaptured at 0x7f2fa5b99348>,
<Element {xmlapi_1.0}periodicTime at 0x7f2fa5b99148>,
<Element {xmlapi_1.0}displayedName at 0x7f2fa5b99088>,
<Element {xmlapi_1.0}monitoredObjectSiteName at 0x7f2fa5b94f48>]

Как я могу получить, например, поле displayedName ...?

Я, например, безуспешно попробовал attrib.get:

In [35]: root.getroot()[0].attrib.get('displayedName').text
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-653606973e5d> in <module>
----> 1 root.getroot()[0].attrib.get('displayedName').text

AttributeError: 'NoneType' object has no attribute 'text'

Это выдержка из файла, который я пытаюсь проанализировать:

<findToFileResponse xmlns="xmlapi_1.0">
    <equipment.SystemCpuMonStats>
        <tmnxSysCpuMonCpuIdle>78.38</tmnxSysCpuMonCpuIdle>
        <tmnxSysCpuMonBusyCoreUtil>21.61</tmnxSysCpuMonBusyCoreUtil>
        <timeCaptured>1587078916040</timeCaptured>
        <children-Set/>
    </equipment.SystemCpuMonStats>
</findToFileResponse>

спасибо!

Ответы [ 2 ]

0 голосов
/ 17 апреля 2020

Так что моя проблема в том, что я имею дело с xml файлами, которые имеют namespaces. Если честно, я действительно не знаю, что означает пространство имен в xml. Тем не менее, это мое решение.

fileCpu = 'file.xml'
from lxml import etree
tree  = etree.parse(fileCpu)
root  = tree.getroot()
xmlns = './/{' + root.nsmap[None] + '}'

for i in root: 
    myValue = i.find(xmlns + 'myKey').text

Мне сначала пришлось идентифицировать пространство имен и использовать его вместе с предполагаемым key для получения желаемого значения.

Я не знаю если есть другое решение для этого, но оно пока работает ...

0 голосов
/ 17 апреля 2020

Например, возьмите xml: https://www.w3schools.com/xml/simple.xml

root = etree.parse("path")

for object in root.findall('food'):
    name = object.find('name')
    price = object.find('price')
    calories = object.find('calories')
    table = {'name': name.text, 'price': price.text, 'calories': calories}
    #Here we changed all the names in the xml to "hi"
    name.text ="hi"
    print(table)

#Here we save the new xml
root.write("path.xml")

В качестве результата мы получим

{'name': 'Belgian Waffles', 'price': '$5.95', 'calories': <Element 'calories' at 0x000002704243A368>}
{'name': 'Strawberry Belgian Waffles', 'price': '$7.95', 'calories': <Element 'calories' at 0x000002704243A548>}
{'name': 'Berry-Berry Belgian Waffles', 'price': '$8.95', 'calories': <Element 'calories' at 0x000002704243A728>}
{'name': 'French Toast', 'price': '$4.50', 'calories': <Element 'calories' at 0x000002704243A8B8>}
{'name': 'Homestyle Breakfast', 'price': '$6.95', 'calories': <Element 'calories' at 0x000002704243AA98>}

Надеюсь, это поможет!

...