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...
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="...
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...
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?
...
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...
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...
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=...
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...
>>> 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...
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...
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...
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 ?
...
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...
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...
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 =...
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 ...
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, ...
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...
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...
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.
...