0

I'm trying to get publish date from XML file. please see my xml file.

<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
  <publshInformation>
    <Publish_Date>06/10/2021</Publish_Date>
    <Record_Count>8954</Record_Count>
  </publshInformation>
</sdnList>

I'm trying below way but its not working and I don't want to use for loop for simple file. Thanks

import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
root = tree.getroot()

publish_Date = root.findall("Publish_Date")

for pdate in publish_Date:
   print(pdate.text)

1 Answer 1

1

You need to use the namespace. see below

import xml.etree.ElementTree as ET

xml = '''<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
 <publshInformation>
   <Publish_Date>06/10/2021</Publish_Date>
   <Record_Count>8954</Record_Count>
 </publshInformation>
</sdnList>'''


root = ET.fromstring(xml)
print(root.find('.//{http://tempuri.org/sdnList.xsd}Publish_Date').text)

output

06/10/2021
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.