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
15
votes
3 answers

ElementTree element index look up

I'm using the xml.etree.ElementTree module to create an XML document with Python 3.1 from another structured document. What ElementTree function can I use that returns the index of an existing sub element?
John
  • 151
  • 1
  • 1
  • 3
14
votes
1 answer

Extracting text after tag in Python's ElementTree

Here is a part of XML: Picture of a cat Extracting the tag is easy. Just do: et = xml.etree.ElementTree.fromstring(our_xml_string) img = et.find('img') But how to get the text immediately after it (Picture of a…
mae
  • 14,947
  • 8
  • 32
  • 47
14
votes
3 answers

How to tell lxml.etree.tostring(element) not to write namespaces in python?

I have a huge xml file (1 Gig). I want to move some of the elements (entrys) to another file with the same header and specifications. Let's say the original file contains this entry with tag :
Aufwind
  • 25,310
  • 38
  • 109
  • 154
14
votes
1 answer

How to prevent xml.ElementTree fromstring from dropping commentnode

I have tho following code fragment: from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for elem in mathml.getiterator(): elem.tag = 'm:' + elem.tag return tostring(mathml) When i input the…
markijbema
  • 3,985
  • 20
  • 32
14
votes
1 answer

How to keep comments while parsing XML using Python / ElementTree

Currently using Python 2.4.3, and not allowed to upgrade I want to change the values of a given attribute in one or more tags, together with XML-comments in the updated file. I have managed to create a Python script that takes a XML-file as…
rhellem
  • 769
  • 1
  • 11
  • 26
14
votes
1 answer

ElementTree Returns Element Instead of ElementTree

I am trying to build an ElementTree from a string. When I do the following (as set out in Python ElementTree: Parsing a string and getting ElementTree instance), I get an Element instead of an ElementTree: companyTree =…
ElToro1966
  • 831
  • 1
  • 8
  • 20
14
votes
3 answers

How to solve TypeError: cannot serialize float Python Elementtree

I got a debugging question. Since I am quite new here, please forgive possible janky walls-of-text. After many hours I finally got elementtree to do what I want, but I cannot output my results, because tree.write("output3.xml") as well as…
sibert
  • 427
  • 2
  • 5
  • 10
14
votes
2 answers

Get parent element after using find method (xml.etree.ElementTree)

I am working with a huge xml-file and try to extract information from different elements. import xml.etree.ElementTree as ET tree = ET.parse('t.xml') root = tree.getroot() To find the elements I use the find method: elm =…
yoopoo
  • 343
  • 1
  • 2
  • 10
14
votes
1 answer

How can I check attribute existings by Elementtree?

I need to check certain attributes for existence. Like: if "blah-blah-blah" is None: print "there is no such attribute" else: print "The attribute exists"
user1464922
  • 371
  • 1
  • 3
  • 10
14
votes
1 answer

lxml etree.iterparse error "TypeError: reading file objects must return plain strings"

I would like to parse an HTML document using lxml. I am using python 3.2.3 and lxml 2.3.4 ( http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml ) I am using the etree.iterparse to parse the document, but it returns the following run-time…
Ababneh A
  • 1,104
  • 4
  • 15
  • 32
13
votes
3 answers

How can one replace an element with text in lxml?

It's easy to completely remove a given element from an XML document with lxml's implementation of the ElementTree API, but I can't see an easy way of consistently replacing an element with some text. For example, given the following input: input =…
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
13
votes
3 answers

xml.etree.ElementTree vs. lxml.etree: different internal node representation?

I have been transforming some of my original xml.etree.ElementTree (ET) code to lxml.etree (lxmlET). Luckily there are a lot of similarities between the two. However, I did stumble upon some strange behaviour that I cannot find written down in any…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
13
votes
1 answer

Write xml utf-8 file with utf-8 data with ElementTree

I'm trying to write an xml file with utf-8 encoded data using ElementTree like this: #!/usr/bin/python # -*- coding: utf-8 -*- …
c0m4
  • 4,343
  • 10
  • 35
  • 40
12
votes
3 answers

Getting all instances of child node using xml.etree.ElementTree

I have the following XML file as input: 0 11
Ash
  • 355
  • 1
  • 2
  • 7
12
votes
1 answer

Writing an XML header with LXML

I'm currently writing a script to convert a bunch of XML files from various encodings to a unified UTF-8. I first try determining the encoding using LXML: def get_source_encoding(self): tree = etree.parse(self.inputfile) encoding =…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411