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
195
votes
7 answers

Parsing XML with namespace in Python via 'ElementTree'

I have the following XML which I want to parse using Python's ElementTree:
Sudar
  • 18,954
  • 30
  • 85
  • 131
178
votes
13 answers

Python ElementTree module: How to ignore the namespace of XML files to locate matching element when using the method "find", "findall"

I want to use the method of findall to locate some elements of the source xml file in the ElementTree module. However, the source xml file (test.xml) has namespaces. I truncate part of xml file as sample:
KevinLeng
  • 1,843
  • 2
  • 12
  • 7
118
votes
6 answers

Convert Python ElementTree to string

Whenever I call ElementTree.tostring(e), I get the following error message: AttributeError: 'Element' object has no attribute 'getroot' Is there any other way to convert an ElementTree object into an XML string? TraceBack: Traceback (most recent…
user1732480
94
votes
11 answers

How to write XML declaration using xml.etree.ElementTree

I am generating an XML document in Python using an ElementTree, but the tostring function doesn't include an XML declaration when converting to plaintext. from xml.etree.ElementTree import Element, tostring document = Element('outer') node =…
Roman Alexander
  • 1,025
  • 1
  • 8
  • 12
90
votes
4 answers

XML parsing - ElementTree vs SAX and DOM

Python has several ways to parse XML... I understand the very basics of parsing with SAX. It functions as a stream parser, with an event-driven API. I understand the DOM parser also. It reads the XML into memory and converts it to objects that can…
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
87
votes
12 answers

access ElementTree node parent node

I am using the builtin Python ElementTree module. It is straightforward to access children, but what about parent or sibling nodes? - can this be done efficiently without traversing the entire tree?
hoju
  • 28,392
  • 37
  • 134
  • 178
77
votes
8 answers

How do I get Python's ElementTree to pretty print to an XML file?

Background I am using SQLite to access a database and retrieve the desired information. I'm using ElementTree in Python version 2.6 to create an XML file with that information. Code import sqlite3 import xml.etree.ElementTree as ET # NOTE: Omitted…
Kimbluey
  • 1,199
  • 2
  • 12
  • 23
71
votes
6 answers

How to extract xml attribute using Python ElementTree

For: text How do I get "value"? xml.findtext("./bar[@key]") Throws an error.
Will Curran
  • 6,959
  • 15
  • 59
  • 92
69
votes
2 answers

Parsing XML in Python using ElementTree example

I'm having a hard time finding a good, basic example of how to parse XML in python using Element Tree. From what I can find, this appears to be the easiest library to use for parsing XML. Here is a sample of the XML I'm working…
Casey
  • 2,025
  • 3
  • 20
  • 22
67
votes
2 answers

Use xml.etree.ElementTree to print nicely formatted xml files

I am trying to use xml.etree.ElementTree to write out xml files with Python. The issue is that they keep getting generated in a single line. I want to be able to easily reference them so if it's possible I would really like to be able to have the…
TheBeardedBerry
  • 1,636
  • 5
  • 20
  • 30
65
votes
5 answers

Saving XML files using ElementTree

I'm trying to develop simple Python (3.2) code to read XML files, do some corrections and store them back. However, during the storage step ElementTree adds this namespace nomenclature. For example: ACTIVE…
ilya1725
  • 4,496
  • 7
  • 43
  • 68
62
votes
2 answers

What are the differences between lxml and ElementTree?

When it comes to generating XML data in Python, there are two libraries I often see recommended: lxml and ElementTree From what I can tell, the two libraries are very similar to each other. They both seem to have similar module names, usage…
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
56
votes
5 answers

lxml etree xmlparser remove unwanted namespace

I have an xml doc that I am trying to parse using Etree.lxml
1
some stuff My code is: path = "path to xml file" from…
Mark
  • 2,522
  • 5
  • 36
  • 42
50
votes
3 answers

how to remove attribute of a etree Element?

I've Element of etree having some attributes - how can we delete the attribute of perticular etree Element.
shahjapan
  • 13,637
  • 22
  • 74
  • 104
50
votes
2 answers

ElementTree XPath - Select Element based on attribute

I am having trouble using the attribute XPath Selector in ElementTree, which I should be able to do according to the Documentation Here's some sample code XML
Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
1
2 3
99 100