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
17
votes
4 answers

How to create with Python's cElementTree

I have tried to use the answer in this question, but can't make it work: How to create "virtual root" with Python's ElementTree? Here's my code: import xml.etree.cElementTree as ElementTree from StringIO import StringIO s = '
tahoar
  • 1,788
  • 3
  • 20
  • 36
17
votes
2 answers

SyntaxError: prefix 'a' not found in prefix map

I'm trying to create a function which counts words in pptx document. The problem is that I can't figure out how to find only this kind of tags: Some Text When I try to: print xmlTree.findall('.//a:t'), it returns SyntaxError: prefix…
Milano
  • 18,048
  • 37
  • 153
  • 353
17
votes
3 answers

Python and ElementTree: return "inner XML" excluding parent element

In Python 2.6 using ElementTree, what's a good way to fetch the XML (as a string) inside a particular element, like what you can do in HTML and javascript with innerHTML? Here's a simplified sample of the XML node I am starting with:
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
17
votes
2 answers

Converting a Python XML ElementTree to a String

I need to convert an XML ElementTree to a String after altering it. It's the toString part that isn't working. import xml.etree.ElementTree as ET tree = ET.parse('my_file.xml') root = tree.getroot() for e in root.iter('tag_name'): e.text =…
user984003
  • 28,050
  • 64
  • 189
  • 285
17
votes
2 answers

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…
Abhishek
  • 1,717
  • 6
  • 22
  • 39
17
votes
1 answer

How to add an element to xml file by using elementtree

I've a xml file, and I'm trying to add additional element to it. the xml has the next structure : What I'm looking for is : but actually I'm getting next xml :
Igal
  • 4,603
  • 14
  • 41
  • 66
16
votes
2 answers

ElementTree's iter() equivalent in Python2.6

I have this code with ElementTree that works well with Python 2.7. I needed to get all the nodes with the name "A" under "X/Y" node. from xml.etree.ElementTree import ElementTree verboseNode = topNode.find("X/Y") nodes =…
prosseek
  • 182,215
  • 215
  • 566
  • 871
16
votes
1 answer

How to preserve namespaces when parsing xml via ElementTree in Python

Assume that I've the following XML which I want to modify using Python's ElementTree: ... I'm doing some modification on the XML file like this: import xml.etree.ElementTree as…
amrezzd
  • 1,787
  • 15
  • 38
16
votes
3 answers

"XML or text declaration not at start of entity: line 2, column 0" when calling ElementTree.parse

ElementTree.parse() fails in the simple example below with the error xml.etree.ElementTree.ParseError: XML or text declaration not at start of entity: line 2, column 0 The XML looks valid and the code is simple, so what am I doing…
frankr6591
  • 1,211
  • 1
  • 8
  • 14
16
votes
1 answer

Can't install elementtree with pip

Trying to install elementtree package with pip. Getting the following error: (taximachine_env)vagrant@dev-gm:/opt/taxi/taxiconsole$ pip install elementtree -vvv Collecting elementtree Getting page https://pypi.python.org/simple/elementtree/ …
Marian Zagoruiko
  • 1,527
  • 3
  • 17
  • 29
16
votes
1 answer

Why does ElementTree reject UTF-16 XML declarations with "encoding incorrect"?

In Python 2.7, when passing a unicode string to ElementTree's fromstring() method that has encoding="UTF-16" in the XML declaration, I'm getting a ParseError saying that the encoding specified is incorrect: >>> from xml.etree import ElementTree >>>…
Henrik Heimbuerger
  • 9,924
  • 6
  • 56
  • 69
16
votes
1 answer

How to correctly parse utf-8 xml with ElementTree?

I need help to understand why parsing my xml file* with xml.etree.ElementTree produces the following errors. *My test xml file contains arabic characters. Task: Open and parse utf8_file.xml file. My first try: import xml.etree.ElementTree as…
minerals
  • 6,090
  • 17
  • 62
  • 107
16
votes
3 answers

Parse XML with (X)HTML entities

Trying to parse XML, with ElementTree, that contains undefined entity (i.e.  ) raises: ParseError: undefined entity   In Python 2.x XML entity dict can be updated by creating parser (documentation): parser =…
theta
  • 24,593
  • 37
  • 119
  • 159
16
votes
2 answers

Python: Ignore xmlns in elementtree.ElementTree

Is there a way to ignore the XML namespace in tage names in elementtree.ElementTree? I try to print all technicalContact tags: for item in root.getiterator(tag='{http://www.example.com}technicalContact'): print item.tag, item.text And I get…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
15
votes
3 answers

lxml convert element to elementtree

The following test code reads a file, and using lxml.html generates the leaf nodes of the DOM/Graph for the page. However, I'm also trying to figure out how to get the input from a "string". Using: lxml.html.fromstring(s) doesn't work, as this…
tom smith
  • 1,035
  • 7
  • 23
  • 39