用Python解析XML文件:检查标记或项目时没有结果
我正在尝试解析从“POST”请求中获得的XML内容。 问题是当我检查'roots'*或项目的'tags'时,它们在应该检查的时候没有指示任何值 XML内容的一部分如下所示:用Python解析XML文件:检查标记或项目时没有结果,python,xml,xml.etree,Python,Xml,Xml.etree,我正在尝试解析从“POST”请求中获得的XML内容。 问题是当我检查'roots'*或项目的'tags'时,它们在应该检查的时候没有指示任何值 XML内容的一部分如下所示: 好啊 4140 2016-01-01T00:00:00-02:00 194.940000 0 4140 2016-02-01T00:00:00-02:00 195.480000 0 4140 2016-03-01T00:00:00-03:00 196.050000 0 4140 2016-04-01T00:00:00-03
好啊
4140
2016-01-01T00:00:00-02:00
194.940000
0
4140
2016-02-01T00:00:00-02:00
195.480000
0
4140
2016-03-01T00:00:00-03:00
196.050000
0
4140
2016-04-01T00:00:00-03:00
195.700000
0
4140
2016-05-01T00:00:00-03:00
198.950000
0
为了处理该文件,我保存了它,并再次加载它以处理内容:
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相关的项时(包含值的标记:
章节内容:
4140
2016-12-01T00:00:00-02:00
203.649516
0
提取/检查值的代码:
root=tree.getroot()
对于root.findall('./NewDataSet/tblIndicePrincipal\u Mes/ValorIndice')中的项:
打印(item.tag())
因此,我得到以下结果(没有显示任何内容)
>对于root.findall('./NewDataSet/tblIndicePrincipal\u Mes/ValorIndice')中的项:
打印(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)
这里
findall()
查找当前元素的直接子元素。xml中的根元素是
,没有名为
findall()
的直接子元素。xml中的根元素是
,没有名为
的直接子元素。
['194.940000', '195.480000', '196.050000', '195.700000', '198.950000']