在python中使用xmltree读取子对象

在python中使用xmltree读取子对象,python,Python,XML正文(response.content) 谢谢 Senthil不是使用findall(),实现这一点的一种方法是使用xml.etree iter()函数 iter()将帮助您将每个子对象作为一个单独的对象。然后,您可以遍历这些对象,并在每个对象后放置一个新行(或在本例中称之为空格)。代码如下所示: subroot = ET.fromstring(response_content) # read from a string variable for entry in subroot.ite

XML正文(response.content)

谢谢 Senthil

不是使用findall(),实现这一点的一种方法是使用xml.etree iter()函数

iter()将帮助您将每个
子对象作为一个单独的对象。然后,您可以遍历这些对象,并在每个对象后放置一个新行(或在本例中称之为空格)。代码如下所示:

subroot = ET.fromstring(response_content) # read from a string variable
for  entry in subroot.iter('column'):
    [print(entry[0][i].text) for i in range(len(entry[0]))]
    print("\n") # any special character you want to print after each object
上述代码的预期输出:

CallType
callVariable1
true


QueueName
callVariable2
true
假设:每个
子级都有相同数量的子级

CallType
callVariable1
true
Left
callVariable2
false
QueueName
callVariable2
true
Right
callVariable4
false
subroot= ET.fromstring(response.content)

for subchild in subroot.findall('./column/entry/'):
    print(subchild.text)

subroot = ET.fromstring(response_content) # read from a string variable
for  entry in subroot.iter('column'):
    [print(entry[0][i].text) for i in range(len(entry[0]))]
    print("\n") # any special character you want to print after each object
CallType
callVariable1
true


QueueName
callVariable2
true