解析 Python 中的 XML 文件:检查标签或项目时无结果

Parse a XML file in Python: No result when checking the Tags or Items

我正在尝试解析从 'POST' 请求中获得的 XML 内容。 问题是当我检查 'roots'* 或项目的 'tags' 时,它应该没有显示值。

部分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 相关的 items(包含值的标签:

栏目内容:

<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>

代码 extract/check 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())
>  

你知道这是怎么回事吗?

这里

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']