Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse all the xml files in a directory one by one using ElementTree

I'm parsing XML in python by ElementTree

import xml.etree.ElementTree as ET 
tree = ET.parse('try.xml')
root = tree.getroot()

I wish to parse all the 'xml' files in a given directory. The user should enter only the directory name and I should be able to loop through all the files in directory and parse them one by one. Can someone tell me the approach. I'm using Linux.

like image 762
Abhishek Avatar asked Mar 28 '13 10:03

Abhishek


People also ask

How do I read a XML file from a directory in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

What is parsing in XML with example?

XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.

What does Etree parse do?

Parsing from strings and files. lxml. etree supports parsing XML in a number of ways and from all important sources, namely strings, files, URLs (http/ftp) and file-like objects. The main parse functions are fromstring() and parse(), both called with the source as first argument.


1 Answers

Just create a loop over os.listdir():

import xml.etree.ElementTree as ET
import os

path = '/path/to/directory'
for filename in os.listdir(path):
    if not filename.endswith('.xml'): continue
    fullname = os.path.join(path, filename)
    tree = ET.parse(fullname)
like image 145
Martijn Pieters Avatar answered Sep 19 '22 18:09

Martijn Pieters