在Python中解析XML文件:检查标记或项时没有结果

2024-09-29 19:27:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解析从“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')

问题是,当我尝试检查与价值相关的项目时(包含值的标记:

章节内容:

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

提取/检查价值的代码:

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

你知道怎么了吗?你知道吗


Tags: nameidbasexmlelementxsxmlnsmes
1条回答
网友
1楼 · 发布于 2024-09-29 19:27:09

这里

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

相关问题 更多 >

    热门问题