Questions tagged [xml.etree]

xml.etree is a part of python's `xml` package, that provides a simple and lightweight `ElementTree` API.

xml.etree is a part of python's xml package, that provides a simple and lightweight ElementTree API.

The module includes tools for parsing XML, searching with XPath expressions, and creating new or modifying existing XML documents.

There is also C implementation available as xml.etree.cElementTree.

288 questions
51
votes
7 answers

etree Clone Node

How to clone Element objects in Python xml.etree? I'm trying to procedurally move and copy (then modify their attributes) nodes.
Ming-Tang
  • 17,410
  • 8
  • 38
  • 76
33
votes
6 answers

ImportError: cannot import name 'etree' on Python 3.6

I am getting error while running "from lxml import tree" on python3.6 >>> import lxml >>> from lxml import etree Traceback (most recent call last): File "", line 1, in ImportError: cannot import name 'etree' The same working on…
Amit Kumar
  • 431
  • 1
  • 4
  • 3
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
10
votes
1 answer

Updating XML elements and attribute values using Python etree

I'm trying to use Python 2.7's ElementTree library to parse an XML file, then replace specific element attributes with test data, then save this as a unique XML file. My idea for a solution was to (1) source new data from a CSV file by reading a…
Nick H
  • 123
  • 1
  • 1
  • 8
10
votes
7 answers

Error using etree in lxml

I want to use xpath in python . I tried import xml.etree.ElementTree as ET Since this library has limited usage I had to use lxml after a long session of search on google. I had several problems during installation and finally i installed lxml but…
Sangamesh
  • 435
  • 3
  • 8
  • 19
9
votes
1 answer

Can .findall() match multiple values in python etree?

Is there a way to match multiple elements in a tree using .findall()? I would like to do this: trees = log.findall('element1' or 'element2') This is my work around (which works in my case because I don't have both e1 and e2 in the same XML): trees…
solbs
  • 940
  • 3
  • 15
  • 29
8
votes
1 answer

How do I use QName (python xml.etree.ElementTree?)

I have done a fair amount of reading on QName but I can't find any good examples of how to use it. Could someone give me a simple example of how to use QName and explain what context it would be used in?
Michael
  • 401
  • 4
  • 9
8
votes
1 answer

How do I get properly escaped XML in python etree untouched?

I'm using python version 2.7.3. test.txt: The tag <StackOverflow> is good to bring up at parties. Result: >>> import xml.etree.ElementTree as ET >>> e =…
user155407
7
votes
4 answers

Removing XML subelement tags with Python using elementTree and .remove()

I need help adjusting my XML file with Python and the elementTree library. For some background, I am not a student and work in industry. I hope to save myself a great deal of manual effort by making these changes automated and typically I would…
jbioni
  • 71
  • 1
  • 1
  • 4
6
votes
1 answer

XML ElementTree - indexing tags

I have a XML file: It has great sushi and even better service. The entire staff was extremely accomodating and tended…
user3058703
  • 571
  • 1
  • 8
  • 22
6
votes
2 answers

get all parents of xml node using python

for this xml 124 A type a Active 125
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
6
votes
2 answers

Creating a Unicode XML from scratch with Python 3.2

So basically, I want to generate an XML with elements generated from data in a python dictionary, where what will come to be tags are the dictionary's keys, and the text the dictionary's values. I have no need to give attributes to the items, and…
mikeybaby173
  • 1,302
  • 2
  • 13
  • 26
5
votes
1 answer

Formatting inserted elements using python xml.etree module, to include new lines

I am inserting a single element into a large xml file. I want the inserted element to be at the top (so I need to use the root.insert method, and can't just append to the file). I would also like the formatting of the element to match the rest of…
thisiscomplex
  • 53
  • 1
  • 5
5
votes
1 answer

How to get following sibling of an etree._Element

I am iterating over elements which I find in a xml file like so: for elem in fileasxml.xpath("//elementname[some condition]"): do something Now instead of do something I'd like to write the element I have found plus the first following-sibling…
pypat
  • 1,096
  • 1
  • 9
  • 19
4
votes
2 answers

parse .xml with prefix's on tags? xml.etree.ElementTree

I can read tags, except when there is a prefix. I'm not having luck searching SO for a previous question. I need to read media:content. I tried image = node.find("media:content"). Rss input: Popular Photography in the last 1…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/python" class="post-tag grid--cell" title="show questions tagged 'python'" rel="tag">python</a> <a href="../../questions/tagged/xml" class="post-tag grid--cell" title="show questions tagged 'xml'" rel="tag">xml</a> <a href="../../questions/tagged/xml.etree" class="post-tag grid--cell" title="show questions tagged 'xml.etree'" rel="tag">xml.etree</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Oct 31 '11 at 01:02">asked Oct 31 '11 at 01:02</time> <a href="../../users/341744/ninmonkey" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/341744.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="ninMonkey" /> </a> <div class="s-user-card--info"> <a href="../../users/341744/ninmonkey" class="s-user-card--link">ninMonkey</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">7,211</li> <li class="s-award-bling s-award-bling__gold" title="8 gold badges">8</li> <li class="s-award-bling s-award-bling__silver" title="37 silver badges">37</li> <li class="s-award-bling s-award-bling__bronze" title="66 bronze badges">66</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="s-pagination pager fr"> <div class="s-pagination--item is-selected">1</div> <a class="s-pagination--item" href="../../questions/tagged/xml.etree_page=2" rel="" title="Go to page 2">2</a> <a class="s-pagination--item" href="../../questions/tagged/xml.etree_page=3" rel="" title="Go to page 3">3</a> <div class="s-pagination--item s-pagination--item__clear">…</div> <a class="s-pagination--item" href="../../questions/tagged/xml.etree_page=19" rel="" title="Go to page 19">19</a> <a class="s-pagination--item" href="../../questions/tagged/xml.etree_page=20" rel="" title="Go to page 20">20</a> <a class="s-pagination--item" href="../../questions/tagged/xml.etree_page=2" rel="next" title="Go to page 2"> Next</a> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>