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
21
votes
6 answers

ElementTree and unicode

I have this char in an xml file: fumè I try to generate an instance of ElementTree with the following code: string_data = open('file.xml') x =…
pistacchio
  • 56,889
  • 107
  • 278
  • 420
21
votes
3 answers

Find element by text with XPath in ElementTree

Given an XML like the following: A B How can I match the element with content A using ElementTree and its support for XPath? Thanks
pistacchio
  • 56,889
  • 107
  • 278
  • 420
20
votes
4 answers

How to set ElementTree Element text field in the constructor

How do I set the text field of of ElementTree Element from its constructor? Or, in the code below, why is the second print of root.text None? import xml.etree.ElementTree as ET root = ET.fromstring("
Steve Schulist
  • 931
  • 1
  • 11
  • 18
19
votes
3 answers

Creating a doctype with lxml's etree

I want to add doctypes to my XML documents that I'm generating with LXML's etree. However I cannot figure out how to add a doctype. Hardcoding and concating the string is not an option. I was expecting something along the lines of how PI's are…
Marijn
  • 288
  • 1
  • 2
  • 6
19
votes
2 answers

How to find XML Elements via XPath in Python in a namespace-agnostic way?

since I had this annoying issue for the 2nd time, I thought that asking would help. Sometimes I have to get Elements from XML documents, but the ways to do this are awkward. I’d like to know a python library that does what I want, a elegant way to…
flying sheep
  • 8,475
  • 5
  • 56
  • 73
19
votes
8 answers

lxml.etree, element.text doesn't return the entire text from an element

I scrapped some html via xpath, that I then converted into an etree. Something similar to this: text1 link text2 but when I call element.text, I only get text1 (It must be there, when I check my query in FireBug, the text of…
user522034
  • 221
  • 1
  • 3
  • 5
19
votes
5 answers

ElementTree TypeError "write() argument must be str, not bytes" in Python3

Got a Problem with generating a .SVG File with Python3 and ElementTree. from xml.etree import ElementTree as et doc = et.Element('svg', width='480', height='360', version='1.1', xmlns='http://www.w3.org/2000/svg') #Doing things with et…
Benny H.
  • 429
  • 1
  • 4
  • 16
19
votes
2 answers

Getting a list of XML tags in file, using xml.etree.ElementTree

As mentioned, I need to get the list of XML tags in file, using library xml.etree.ElementTree. I am aware that there are properties and methods like ETVar.child, ETVar.getroot(), ETVar.tag, ETVar.attrib. But to be able to use them and get at least…
FanaticD
  • 1,416
  • 4
  • 20
  • 36
19
votes
7 answers

Check if XML Element has children or not, in ElementTree

I retrieve an XML documents this way: import xml.etree.ElementTree as ET root = ET.parse(urllib2.urlopen(url)) for child in root.findall("item"): a1 = child[0].text # ok a2 = child[1].text # ok a3 = child[2].text # ok a4 = child[3].text #…
Incerteza
  • 32,326
  • 47
  • 154
  • 261
19
votes
3 answers

Cannot write XML file with default namespace

I'm writing a Python script to update Visual Studio project files. They look like this:
Andomar
  • 232,371
  • 49
  • 380
  • 404
18
votes
1 answer

Pylint Error Message: "E1101: Module 'lxml.etree' has no 'strip_tags' member'"

I am experimenting with lxml and python for the first time for a personal project, and I am attempting to strip tags from a bit of source code using etree.strip_tags(). For some reason, I keep getting the error message: "E1101: Module 'lxml.etree'…
Aaron Viscichini
  • 257
  • 3
  • 13
18
votes
2 answers

Saving XML using ETree in Python. It's not retaining namespaces, and adding ns0, ns1 and removing xmlns tags

I see there are similar questions here, but nothing that has totally helped me. I've also looked at the official documentation on namespaces but can't find anything that is really helping me, perhaps I'm just too new at XML formatting. I understand…
emmdee
  • 1,541
  • 3
  • 25
  • 46
18
votes
3 answers

How do I set attributes for an XML element with Python?

I am using ElementTree to build an XML file. When I try to set an element's attribute with ET.SubElement().__setattr__(), I get the error AttributeError: __setattr__. import xml.etree.cElementTree as ET summary = open(Summary.xml, 'w') root =…
Venkatesh
  • 3,558
  • 8
  • 32
  • 38
18
votes
3 answers

Merge xml files with nested elements without external libraries

I am trying to merge multiple XML files together using Python and no external libraries. The XML files have nested elements. Sample File 1: textA text now
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
18
votes
2 answers

Alter namespace prefixing with ElementTree in Python

By default, when you call ElementTree.parse(someXMLfile) the Python ElementTree library prefixes every parsed node with it's namespace URI in Clark's Notation: {http://example.org/namespace/spec}mynode This makes accessing specific nodes by…
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88