Разбор XML-файла в Python: нет результатов при проверке тегов или элементов - PullRequest
0 голосов
/ 01 июля 2019

Я пытаюсь проанализировать содержимое XML, полученное по запросу «POST».Проблема заключается в том, что я проверяю «корни» * или «теги» элемента , которые не указывают значения, когда это необходимо.

Часть содержимого XMLследующее:


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <fntDadoMensalComDataResponse xmlns="https://app.Econdrivers.com/WSEDiario">
            <fntDadoMensalComDataResult>
                <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="tblStatus">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="Status" type="xs:string" minOccurs="0" />
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                                <xs:element name="tblIndicePrincipal_Mes">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="IndicePrincipalID" type="xs:int" minOccurs="0" />
                                            <xs:element name="DataIndice" type="xs:dateTime" minOccurs="0" />
                                            <xs:element name="ValorIndice" type="xs:decimal" minOccurs="0" />
                                            <xs:element name="Base" type="xs:unsignedByte" minOccurs="0" />
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:schema>
                <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
                    <NewDataSet xmlns="">
                        <tblStatus diffgr:id="tblStatus1" msdata:rowOrder="0">
                            <Status>Ok</Status>
                        </tblStatus>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes1" msdata:rowOrder="0">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-01-01T00:00:00-02:00</DataIndice>
                            <ValorIndice>194.940000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes2" msdata:rowOrder="1">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-02-01T00:00:00-02:00</DataIndice>
                            <ValorIndice>195.480000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes3" msdata:rowOrder="2">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-03-01T00:00:00-03:00</DataIndice>
                            <ValorIndice>196.050000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes4" msdata:rowOrder="3">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-04-01T00:00:00-03:00</DataIndice>
                            <ValorIndice>195.700000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes5" msdata:rowOrder="4">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-05-01T00:00:00-03:00</DataIndice>
                            <ValorIndice>198.950000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                    </NewDataSet>
                </diffgr:diffgram>
            </fntDadoMensalComDataResult>
        </fntDadoMensalComDataResponse>
    </soap:Body>
</soap:Envelope>

Чтобы обработать файл, я сохранил его и снова загрузил для работы с содержимым:


    with open('C://Users/xyz/Documents/topnewsfeed.xml', 'wb') as f:
        f.write(response.content)
    f.close()

    tree = ET.parse('C://Users/xyz/Documents/topnewsfeed.xml')

Проблема заключается в том, что при попытке проверить элементы , относящиеся к ValorIndice (тег, содержащий значение:

Содержание раздела:

<tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes12" msdata:rowOrder="11">
<IndicePrincipalID>4140</IndicePrincipalID>
<DataIndice>2016-12-01T00:00:00-02:00</DataIndice>
<ValorIndice>203.649516</ValorIndice>
<Base>0</Base>

Код для извлечения / проверки ValorIndice :

root = tree.getroot()

for item in root.findall('./NewDataSet/tblIndicePrincipal_Mes/ValorIndice'):
    print(item.tag())

В результате я получаю следующее (ничего не появляется)


> for item in root.findall('./NewDataSet/tblIndicePrincipal_Mes/ValorIndice'):
    print(item.tag())
>  

Знаете, что не так?

1 Ответ

2 голосов
/ 01 июля 2019

Здесь

import xml.etree.ElementTree as ET
import re

data = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <fntDadoMensalComDataResponse xmlns="https://app.Econdrivers.com/WSEDiario">
            <fntDadoMensalComDataResult>
                <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="tblStatus">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="Status" type="xs:string" minOccurs="0" />
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                                <xs:element name="tblIndicePrincipal_Mes">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="IndicePrincipalID" type="xs:int" minOccurs="0" />
                                            <xs:element name="DataIndice" type="xs:dateTime" minOccurs="0" />
                                            <xs:element name="ValorIndice" type="xs:decimal" minOccurs="0" />
                                            <xs:element name="Base" type="xs:unsignedByte" minOccurs="0" />
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:schema>
                <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
                    <NewDataSet xmlns="">
                        <tblStatus diffgr:id="tblStatus1" msdata:rowOrder="0">
                            <Status>Ok</Status>
                        </tblStatus>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes1" msdata:rowOrder="0">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-01-01T00:00:00-02:00</DataIndice>
                            <ValorIndice>194.940000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes2" msdata:rowOrder="1">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-02-01T00:00:00-02:00</DataIndice>
                            <ValorIndice>195.480000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes3" msdata:rowOrder="2">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-03-01T00:00:00-03:00</DataIndice>
                            <ValorIndice>196.050000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes4" msdata:rowOrder="3">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-04-01T00:00:00-03:00</DataIndice>
                            <ValorIndice>195.700000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                        <tblIndicePrincipal_Mes diffgr:id="tblIndicePrincipal_Mes5" msdata:rowOrder="4">
                            <IndicePrincipalID>4140</IndicePrincipalID>
                            <DataIndice>2016-05-01T00:00:00-03:00</DataIndice>
                            <ValorIndice>198.950000</ValorIndice>
                            <Base>0</Base>
                        </tblIndicePrincipal_Mes>
                    </NewDataSet>
                </diffgr:diffgram>
            </fntDadoMensalComDataResult>
        </fntDadoMensalComDataResponse>
    </soap:Body>
</soap:Envelope>
    '''

root = ET.fromstring(data)
values = [x.text for x in root.findall('.//ValorIndice')]
print(values)

вывод

['194.940000', '195.480000', '196.050000', '195.700000', '198.950000']
...