0

I'm using ElemetTree to iterate all the xml tag. Whereas some of my xml contents look like below xml. When iterating the xml getting the <result1> tag text as None not "This is the Result of Chrome Browser"

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

Python

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

O/P Getting

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 Wanted

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!

Please Help!!

  • 1
    "This is the Result of Chrome Browser" is the `tail` of the `

    ` element. https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.tail

    – mzjn Mar 23 '23 at 10:18

1 Answers1

1

You can search for elem.tag, elem.attrib, elem.text and elem.tail:

import xml.etree.ElementTree as ET

xml_string ="""<?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>
"""
tree = ET.ElementTree(ET.fromstring(xml_string))

for elem in tree.iter():
    print("TAG: ",elem.tag,'\n', "Text: ", elem.text, '\t\n', "Tail:", elem.tail, '\n\n')
Hermann12
  • 1,709
  • 2
  • 5
  • 14