如何使用 ElementTree 在 Python 中获取下面提到的 xml 的值?

问题描述 投票:0回答:0

我正在使用 ElemetTree 来迭代所有的 xml 标签。而我的一些 xml 内容看起来像下面的 xml。当迭代 xml 时,

<result1>
标记文本为 None 而不是“这是 Chrome 浏览器的结果”

<?xml version="1.0" encoding="UTF-8"?>
<article>
 <result1 id="val1"><h2>Google Chrome</h2>
 This is the Result of Chrome Browser<p>Google Chrome is a web browser developed by Google, released in 2008. Chrome is the world's 
  most popular web browser today!</p></result1>
</article>

蟒蛇

import xml.etree.ElementTree as ET
treexml = ET.parse('exam.xml')
for elemintree in treexml.iter():
    print(elemintree.tag,elemintree.text)

O/P 获取

article
result1 None
h2 Google Chrome
p Google Chrome is a web browser developed by Google, released in 2008. Chrome is the world's 
  most popular web browser today!

需要O / P

h2 Google Chrome
This is the Result of Chrome Browser
p Google Chrome is a web browser developed by Google, released in 2008. Chrome is the world's 
  most popular web browser today!

请帮助!!

python xml elementtree
© www.soinside.com 2019 - 2024. All rights reserved.