Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to parse xml throws FileNotFoundError

New to python and all I am doing is parsing a simple xml string. But when I do that, it says ‘No such file or directory’ at Et.parse. I also tried saying Et.parse(Et.fromstring(xmlfile)) but still that leads to a slightly different exception. Please help.xml parse error

import xml.etree.ElementTree as ET
    
    
thisDict={}
    
def parseXML(xmlfile):
    tree = ET.parse(xmlfile)
    root=tree.getroot()
    for package in root.findall('package'):
    if package is None:
        continue
    if 'id' in package.attrib:
       thisDict['package']=package.get('id')
    if 'version' in package.attrib:
       thisDict['version']=package.get('version')

    for x, y in thisDict.items():
    print(x, y)


xmlstr=f'''<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="5.1.1" targetFramework="net481" />
<package id="Moq" version="4.18.4" targetFramework="net481" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net481" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net481" />
<package id="xunit" version="2.4.2" targetFramework="net481" />
<package id="xunit.abstractions" version="2.0.3" targetFramework="net481" />
<package id="xunit.analyzers" version="1.0.0" targetFramework="net481" />
<package id="xunit.assert" version="2.4.2" targetFramework="net481" />
<package id="xunit.core" version="2.4.2" targetFramework="net481" />
<package id="xunit.extensibility.core" version="2.4.2" targetFramework="net481" />
<package id="xunit.extensibility.execution" version="2.4.2" targetFramework="net481" />
<package id="xunit.runner.visualstudio" version="2.4.5" targetFramework="net481" developmentDependency="true" />
</packages>'''

    
parseXML(xmlstr)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since the xml is a string you should use ET.fromstring.

For me this worked, but it means that you should adjust your code to not read the file:

xml = '''<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Castle.Core" version="5.1.1" targetFramework="net481" />
  <package id="Moq" version="4.18.4" targetFramework="net481" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net481" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net481" />
  <package id="xunit" version="2.4.2" targetFramework="net481" />
  <package id="xunit.abstractions" version="2.0.3" targetFramework="net481" />
  <package id="xunit.analyzers" version="1.0.0" targetFramework="net481" />
  <package id="xunit.assert" version="2.4.2" targetFramework="net481" />
  <package id="xunit.core" version="2.4.2" targetFramework="net481" />
  <package id="xunit.extensibility.core" version="2.4.2" targetFramework="net481" />
  <package id="xunit.extensibility.execution" version="2.4.2" targetFramework="net481" />
  <package id="xunit.runner.visualstudio" version="2.4.5" targetFramework="net481" developmentDependency="true" />
</packages>'''
tree = ET.fromstring(xml)
tree.findall('package')

If you want to use your function you could use theio package:

import io

def parseXML(xmlfile):
    tree = ET.parse(xmlfile)
    root=tree.getroot()
    thisDict = {}
    for package in root.findall('package'):
        if package is None:
            continue
        if 'id' in package.attrib:
            thisDict['package']=package.get('id')
        if 'version' in package.attrib:
            thisDict['version']=package.get('version')

    for x, y in thisDict.items():
        print(x, y)

parseXML(io.StringIO(xml))

note that I added thisDict = {} to your function. But it probably does not what you want, since the 'package' key is overwritten every time a new package is found.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading