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
33
votes
12 answers

Can ElementTree be told to preserve the order of attributes?

I've written a fairly simple filter in python using ElementTree to munge the contexts of some xml files. And it works, more or less. But it reorders the attributes of various tags, and I'd like it to not do that. Does anyone know a switch I can…
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
33
votes
4 answers

Error 'failed to load external entity' when using Python lxml

I'm trying to parse an XML document I retrieve from the web, but it crashes after parsing with this error: ': failed to load external entity "
daveeloo
  • 923
  • 3
  • 9
  • 8
30
votes
1 answer

ElementTree findall() returning empty list

I am trying to write a small script for interacting with the last.fm API. I have a small bit of experience working with ElementTree, but the way I used it previously doesn't seem to be working, it instead returns an empty list. I removed the API…
Cirno
  • 335
  • 1
  • 4
  • 8
30
votes
8 answers

Remove whitespaces in XML string

How can I remove the whitespaces and line breaks in an XML string in Python 2.6? I tried the following packages: etree: This snippet keeps the original whitespaces: xmlStr = ''' ''' xmlElement…
desolat
  • 4,123
  • 6
  • 36
  • 47
27
votes
4 answers

Suppressing namespace prefixes in ElementTree 1.2

In python 2.7 (with etree 1.3), I can suppress the XML prefixes on elements like this: Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright",…
jterrace
  • 64,866
  • 22
  • 157
  • 202
27
votes
4 answers

Is there a way to get a line number from an ElementTree Element

So I'm parsing some XML files using Python 3.2.1's cElementTree, and during the parsing I noticed that some of the tags were missing attribute information. I was wondering if there is any easy way of getting the line numbers of those Elements in the…
John Smith
  • 273
  • 1
  • 3
  • 4
27
votes
3 answers

Python ElementTree default namespace?

Is there a way to define the default/unprefixed namespace in python ElementTree? This doesn't seem to work... ns = {"":"http://maven.apache.org/POM/4.0.0"} pom = xml.etree.ElementTree.parse("pom.xml") print(pom.findall("version", ns)) Nor does…
Robert Fraser
  • 10,649
  • 8
  • 69
  • 93
26
votes
5 answers

How to find the number of elements in element tree in python?

I am new to element tree,here i am trying to find the number of elements in the element tree. from lxml import etree root = etree.parse(open("file.xml",'r')) is there any way to find the total count of the elements in root?
mariz
  • 509
  • 1
  • 7
  • 13
26
votes
2 answers

ElementTree iterparse strategy

I have to handle xml documents that are big enough (up to 1GB) and parse them with python. I am using the iterparse() function (SAX style parsing). My concern is the following, imagine you have an xml like this
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
25
votes
2 answers

Access nested children in xml file parsed with ElementTree

I am new to xml parsing. This xml file has the following tree: FHRSEstablishment |--> Header | |--> ... |--> EstablishmentCollection | |--> EstablishmentDetail | | |-->... | |--> Scores | | |-->... |-->…
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
25
votes
3 answers

Accessing XMLNS attribute with Python Elementree?

How can one access NS attributes through using ElementTree? With the following: When I try to root.get('xmlns') I get back None, Category…
Melchior
  • 251
  • 1
  • 3
  • 3
23
votes
1 answer

Python running out of memory parsing XML using cElementTree.iterparse

A simplified version of my XML parsing function is here: import xml.etree.cElementTree as ET def analyze(xml): it = ET.iterparse(file(xml)) count = 0 for (ev, el) in it: count += 1 print('count: {0}'.format(count)) This…
Aillyn
  • 23,354
  • 24
  • 59
  • 84
23
votes
3 answers

Python ElementTree parsing unbound prefix error

I am learning ElementTree in python. Everything seems fine except when I try to parse the xml file with prefix: test.xml:
Kintarō
  • 2,947
  • 10
  • 48
  • 75
22
votes
2 answers

Python ElementTree: Parsing a string and getting ElementTree instance

I have a string containing XML data that is returned from an http request. I am using ElementTree to parse the data, and I want to then search recursively for an element. According to this question, I can only search recursively with…
YXD
  • 31,741
  • 15
  • 75
  • 115
21
votes
2 answers

Empty list returned from ElementTree findall

I'm new to xml parsing and Python so bear with me. I'm using lxml to parse a wiki dump, but I just want for each page, its title and text. For now I've got this: from xml.etree import ElementTree as etree def parser(file_name): document =…
liloka
  • 1,016
  • 4
  • 14
  • 29