I'm trying to use xml.etree.ElementTree in python but it works for sample code, but doesn't work for my other code.
Ex: XML FILE: PROGRAM IS WORKING FINE WITH THIS XML FILE:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
Program:
import xml.etree.ElementTree as ET
tree = ET.parse('country.xml')
root = tree.getroot()
for page in root.findall('country'):
print("inside")
OutPut:
inside
inside
inside
This doesn't work for below program:
XML File: **PROGRAM IS NOT WORKING WITH THE BELOW XML FILE**
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.8/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.8/ http://www.mediawiki.org/xml/export-0.8.xsd" version="0.8" xml:lang="en">
<siteinfo>
<sitename>Wikipedia</sitename>
<base>http://en.wikipedia.org/wiki/Main_Page</base>
<generator>MediaWiki 1.23wmf11</generator>
<case>first-letter</case>
<namespaces>
<namespace key="-2" case="first-letter">Media</namespace>
</namespaces>
</siteinfo>
<page>
<title>Affirming the consequent</title>
<ns>0</ns>
<id>675</id>
</page>
</mediawiki>
Code:
import xml.etree.ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()
for page in root.findall('page'):
print("inside")
Output: No Output.
I figured out the reason is due to attributes in mediawiki
tag. But I can't avoid that tag in my sample data. Is there any possible way to make this work.