Reputation: 4787
Parsing an xml file (svg in this case) in Python is convenient, but as soon as there are namespaces involved, nothing works. What is the right way to work with Pythons xml library?
If my file had no namespace, I could easily do the following code and get all the elements:
import xml.etree.ElementTree as ET
tree = ET.parse('model1.svg')
root = tree.getroot()
lst = root.findall('g/g/g/g')
print(lst)
But since it has a namespace:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="temp" width="1809.6200256347656" height="1247.809829711914" version="1.1" viewBox="0 0 1809.6200256347656 1247.809829711914">
The response is: []
If I try to print the root
I get this :
<Element '{http://www.w3.org/2000/svg}svg' at 0x7fbc45154ea8>
instead of this:
<Element 'svg' at 0x7f8ee9377368>
So I just can't work with it. How can I deactivate/ignore it?
Upvotes: 2
Views: 2253
Reputation: 4787
The solution is using the xml tags (e.g. g
) with a prefix from a predefined namespace array:
import xml.etree.ElementTree as ET
tree = ET.parse('./model1.svg')
root = tree.getroot()
ns_array = {
'svg': 'http://www.w3.org/2000/svg',
'xlink': 'http://www.w3.org/1999/xlink'
}
lst = root.findall('svg:g/svg:g/svg:g/svg:g', ns_array)
Upvotes: 3