elementtree

insert tags in ElementTree text

I am using the Python ElementTree module to manipulate HTML. I want to emphasize certain words, and my current solution is: for e in tree.getiterator(): for attr in 'text', 'tail': words = (getattr(e, attr) or '').split() change = False for i, word in enumerate(words): word = clean_word.sub('', wo...

How to use "class" the word as parameter function calls in python

I am writing an XML generator per my manager's request. For less typings' sake, I decided using ElementTree as parser and SimpleXMLWriter as writer. The result XML require attributes named "class". e.g. <Node class="oops"></Node> As the official tutorial suggested, to write an XML node just use this method: w.element("meta", name="...

Read XML with multiple top-level items using Python ElementTree?

How can I read an XML file using Python ElementTree, if the XML has multiple top-level items? I have an XML file that I would like to read using Python ElementTree. Unfortunately, it has multiple top-level tags. I would wrap <doc>...</doc> around the XML, except I have to put the <doc> after the <?xml> and <!DOCTYPE> fields. But figuri...

access ElementTree node parent node

I am using the builtin Python ElementTree module. It is straightforward to access children, but what about parent nor sibling nodes? - can this be done efficiently without traversing the entire tree? ...

Repeatedly querying xml using python

I have some xml documents I need to run queries on. I've created some python scripts (using ElementTree) to do this, since I'm vaguely familiar with using it. The way it works is I run the scripts several times with different arguments, depending on what I want to find out. These files can be relatively large (10MB+) and so it takes r...

How do I create new xml from ElementTree?

Bit of a beginner question here: Say I have a block of xml: <root> <district> <house><room><door/><room></house> </district> <district> <street> <house>and so on</house> </street> etc. With ElementTree I can do: houses=doc.findall(".//house") to select all the house nodes, regardless of their parent. What I want to do...

: in node causing Keyerror in xmlparsing using ElementTree

Hi I'm using ElementTree to parse out an xml feed from Kuler. I'm only beginning in python but am stuck here. The parsing works fine until I attempt to retrieve any nodes containing ':' e.g kuler:swatchHexColor Below is a cut down version of the full feed but same structure: <rss xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:kuler=...

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 throw to make it keep them in specified order? Context for this I'm working with and on a...

SyntaxError using gdata-python-client to access Google Book Search Data API

>>> import gdata.books.service >>> service = gdata.books.service.BookService() >>> results = service.search_by_keyword(isbn='0434003484') Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> results = service.search_by_keyword(isbn='0434003484') ... snip ... File "C:\Python26\lib\site-packages\atom\__init__.py", l...

How to update the text of a tag in XML using Elementree

Using elementree, the easiest way to read the text of a tag is to do the following: import elementtree.ElementTree as ET sKeyMap = ET.parse("KeyMaps/KeyMap_Checklist.xml") host = sKeyMap.findtext("/BrowserInformation/BrowserSetup/host") Now I want to update the text in the same file, hopefully without having to re-write it with someth...

Python + Expat: Error on &#0; entities

I have written a small function, which uses ElementTree and xpath to extract the text contents of certain elements in an xml file: #!/usr/bin/env python2.5 import doctest from xml.etree import ElementTree from StringIO import StringIO def parse_xml_etree(sin, xpath): """ Takes as input a stream containing XML and an XPath expression...

In Python ElementTree how can I get list of all ancestors of an element in tree ?

I need "get_ancestors_recursively" function. A sample run can be >>> dump(tr) <anc1> <anc2> <element> </element> </anc2> </anc1> >>> input_element = tr.getiterator("element")[0] >>> get_ancestors_recursively(input_element) ['anc1', 'anc2'] Can somebody help me with this ? ...

Can't parse XML effectively using Python

import urllib import xml.etree.ElementTree as ET def getWeather(city): #create google weather api url url = "http://www.google.com/ig/api?weather=" + urllib.quote(city) try: # open google weather api url f = urllib.urlopen(url) except: # if there was an error opening the url, return retur...

Python 2.5.4 - ImportError: No module named etree.ElementTree

Hi, I'm running Python 2.5.4 on Windows and I keep getting an error when trying to import the ElementTree or cElementTree modules. The code is very simple (I'm following a tutorial): import xml.etree.ElementTree as xml root = xml.Element('root') child = xml.Element('child') root.append(child) child.attrib['name'] = "Charlie" file = ope...

Reading, changing, and writing values to/from an XML file with ElementTree

Using ElementTree, I want to do the following: 1 - Open an XML file and read the text of a tag (works). 2 - Print out that value then change the value (works). 3 - Write the changed value back to the same tag in the XML file (Doesn't work). import elementtree.ElementTree as ET sKeyMap = ET.parse("KeyMap_Checklist.xml") port_element =...

Multiple text nodes in Python's ElementTree? HTML generation.

I'm using ElementTree to generate some HTML, but I've run into the problem that ElementTree doesn't store text as a Node, but as the text and tail properties of Element. This is a problem if I want to generate something that would require multiple text nodes, for example: <a>text1 <b>text2</b> text3 <b>text4</b> text5</a> As far as I ...

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 = '''<root> <head></head> <content></content> </root>''' xmlElement = xml.etree.ElementTree.XML(xmlStr) xmlStr = xml.etree.ElementTree.tostring(xmlElement, ...

XML reading using ElementTree

Hi, I have one xml file. <Item>Item value</Item> <Itemdate>24/07/2010</Itemdate> <Total>1</Total> <Itemcategory>Income</Itemcategory> <GroupName>Salary</GroupName> <EditId>undefined</EditId> <Item>Item value</Item> <Itemdate>24/07/2010</Itemdate> <Total>1</Total> <Itemcategory>Income</Itemcategory> <GroupName>Salary</GroupNam...

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: <label attr="foo" attr2="bar">This is some text <a href="foo.htm">and a link</a> in embedded HTML</lab...

how to return the element tree instance.

I want to generate a xml file. I have written a xml_generator method. When /xxx url hits i call this generator function. how should i return this Because returning instance of generator function creates an error. ...