Questions tagged [elementtree]

ElementTree is a Python library for creating and parsing XML.

ElementTree is a Python library for creating and parsing XML.

from xml.etree import ElementTree

An alternative implementation on top of libxml and libxslt, with an expanded API is lxml, see the tag.

2858 questions
48
votes
5 answers

Using XPath in ElementTree

My XML file looks like the following: 2260
Ryan R. Rosario
  • 5,114
  • 9
  • 41
  • 56
45
votes
1 answer

How can I check the existence of attributes and tags in XML before parsing?

I'm parsing an XML file via Element Tree in python and and writing the content to a cpp file. The content of children tags will be variant for different tags. For example first event tag has party tag as child but second event tag doesn't have.…
Abhishek
  • 1,717
  • 6
  • 22
  • 39
42
votes
2 answers

Emitting namespace specifications with ElementTree in Python

I am trying to emit an XML file with element-tree that contains an XML declaration and namespaces. Here is my sample code: from xml.etree import ElementTree as ET ET.register_namespace('com',"http://www.company.com") #some name # build a tree…
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
41
votes
6 answers

Using Python Iterparse For Large XML Files

I need to write a parser in Python that can process some extremely large files ( > 2 GB ) on a computer without much memory (only 2 GB). I wanted to use iterparse in lxml to do it. My file is of the format: Item 1
Dave Johnshon
  • 475
  • 1
  • 7
  • 6
41
votes
10 answers

Comparing XML in a unit test in Python

I have an object that can build itself from an XML string, and write itself out to an XML string. I'd like to write a unit test to test round tripping through XML, but I'm having trouble comparing the two XML versions. Whitespace and attribute order…
Adam Endicott
  • 634
  • 1
  • 6
  • 8
40
votes
12 answers

Converting xml to dictionary using ElementTree

I'm looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes.
Mokus
  • 10,174
  • 18
  • 80
  • 122
40
votes
5 answers

How to get all sub-elements of an element tree with Python ElementTree?

I want to find a way to get all the sub-elements of an element tree like the way ElementTree.getchildren() does, since getchildren() is deprecated since Python version 2.7. I don't want to use it anymore, though I can still use it currently.
j5shi
  • 797
  • 1
  • 8
  • 21
38
votes
1 answer

Python - ElementTree- cannot use absolute path on element

I'm getting this error in ElementTree when I try to run the code below: SyntaxError: cannot use absolute path on element My XML document looks like this: