本文整理汇总了Python中xml.etree.cElementTree.ElementTree.parse方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.parse方法的具体用法?Python ElementTree.parse怎么用?Python ElementTree.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.cElementTree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getEvents
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def getEvents(feed):
"""
Creates events from an ATOM feed with GeoRSS points.
"""
events = []
tree = ElementTree()
tree.parse(feed)
entries = ElementTree(tree).iter('{http://www.w3.org/2005/Atom}entry')
for entry in entries:
author = entry.find('{http://www.w3.org/2005/Atom}author')
try:
name = author.find('{http://www.w3.org/2005/Atom}name').text
uri = author.find('{http://www.w3.org/2005/Atom}uri').text
except AttributeError:
continue
try:
point = entry.find('{http://www.georss.org/georss}point').text
latitude = point.split()[0]
longitude = point.split()[1]
except AttributeError:
continue
published = parse_date(
entry.find('{http://www.w3.org/2005/Atom}published').text
)
event = Event(name, uri, published, latitude, longitude)
events.append(event)
return events
示例2: tweak_build_xml
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def tweak_build_xml(self):
runjdwp_args = [
'transport=dt_socket',
'server=y',
'address=8765',
'suspend=n',
]
runjdwp_args = ','.join(runjdwp_args)
jvm_debug_args = [
'-Xdebug',
'-Xrunjdwp:%s' % (runjdwp_args,),
]
jvm_debug_args = ' '.join(jvm_debug_args)
build_xml = self.get_build_xml()
tree = ElementTree()
tree.parse(build_xml)
root = tree.getroot()
targets = root.findall('target')
for node in targets:
if node.get('name') == 'run':
java_node = node.find('java')
SubElement(java_node, 'jvmarg', {
'line': jvm_debug_args,
})
tree.write(build_xml)
示例3: extractElement
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def extractElement(self, elementpath, respdata):
try:
tree = ElementTree()
tree.parse(StringIO(respdata))
except:
return None
# Strip off the top-level item
if elementpath[0] == '/':
elementpath = elementpath[1:]
splits = elementpath.split('/', 1)
root = splits[0]
if tree.getroot().tag != root:
return None
elif len(splits) == 1:
return tree.getroot().text
else:
elementpath = splits[1]
e = tree.find(elementpath)
if e is not None:
return e.text
else:
return None
示例4: updatebq
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def updatebq(self):
from xml.etree.cElementTree import ElementTree
tree = ElementTree()
tree.parse(GSXML)
tvlist = []
for iptv in tree.findall("iptv"):
name = iptv.findtext("name").title()
(protocol, serviceType, bufferSize, epgId) = iptv.findtext("type").split(":")
uri = iptv.findtext("uri")
if protocol in "livestreamer":
uri = "http://localhost:88/" + uri
uri = uri.replace(":", "%3a")
service = "#SERVICE {s}:0:1:{e}:{e}:0:0:0:0:0:{u}:{n}\n".format(s=serviceType,e=epgId,u=uri,n=name)
tvlist.append((name,service))
tvlist=sorted(tvlist, key=lambda channel: channel[0]) #sort by name
with open(GSBQ, "w") as f:
f.write("#NAME GreekStreamTV\n")
for (name, service) in tvlist:
f.write(service)
com = "cat /usr/lib/enigma2/python/Plugins/Satdreamgr/UpdateBouquet/stream.xml ; rm /usr/lib/enigma2/python/Plugins/Satdreamgr/UpdateBouquet/stream.xml"
out = os.popen(com)
return list
示例5: list_articles
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def list_articles(target_directory, supplementary_materials=False, skip=[]):
listing = listdir(target_directory)
for filename in listing:
result_tree = ElementTree()
result_tree.parse(path.join(target_directory, filename))
for tree in result_tree.iterfind('article'):
pmcid = _get_pmcid(tree)
if pmcid in skip:
continue
result = {}
result['name'] = pmcid
result['doi'] = _get_article_doi(tree)
result['article-categories'] = _get_article_categories(tree)
result['article-contrib-authors'] = _get_article_contrib_authors(tree)
result['article-title'] = _get_article_title(tree)
result['article-abstract'] = _get_article_abstract(tree)
result['journal-title'] = _get_journal_title(tree)
result['article-year'], \
result['article-month'], \
result['article-day'] = _get_article_date(tree)
result['article-url'] = _get_article_url(tree)
result['article-license-url'], \
result['article-license-text'], \
result['article-copyright-statement'] = _get_article_licensing(tree)
result['article-copyright-holder'] = _get_article_copyright_holder(tree)
if supplementary_materials:
result['supplementary-materials'] = _get_supplementary_materials(tree)
yield result
示例6: pagetext_search
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def pagetext_search(self, locator, query, rows=None, start=None):
"""Does an advanced search on
pagetext:blah locator:identifier
where identifier is one of the id's from fulltext search.
You get back a list of page numbers like [21, 25, 39]."""
def extract(page_id):
"""TODO: DjVu format is deprecated. Is this function
still even used?
A page id is something like
'adventsuburbanit00butlrich_0065.djvu',
which this function extracts asa a locator and
a leaf number ('adventsuburbanit00butlrich', 65). """
g = re.search('(.*)_(\d{4})\.djvu$', page_id)
a,b = g.group(1,2)
return a, int(b)
# try using qf= parameter here and see if it gives a speedup. @@
# pdb.set_trace()
query = self._prefix_query('pagetext', query)
page_hits = self.raw_search(query,
fq='locator:' + locator,
rows=rows,
start=start)
XML = ElementTree()
try:
XML.parse(StringIO(page_hits))
except SyntaxError as e:
raise SolrError(e)
page_ids = list(e.text for e in XML.getiterator('identifier'))
return [extract(x)[1] for x in page_ids]
示例7: list_articles
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def list_articles(target_directory, supplementary_materials=False, skip=[]):
"""
Iterates over archive files in target_directory, yielding article information.
"""
listing = listdir(target_directory)
for filename in listing:
with tarfile.open(path.join(target_directory, filename)) as archive:
for item in archive:
if item.name in skip:
continue
if path.splitext(item.name)[1] == '.nxml':
content = archive.extractfile(item)
tree = ElementTree()
tree.parse(content)
result = {}
result['name'] = item.name
result['article-contrib-authors'] = _get_article_contrib_authors(tree)
result['article-title'] = _get_article_title(tree)
result['article-abstract'] = _get_article_abstract(tree)
result['journal-title'] = _get_journal_title(tree)
result['article-date'] = _get_article_date(tree)
result['article-url'] = _get_article_url(tree)
result['article-license-url'] = _get_article_license_url(tree)
result['article-copyright-holder'] = _get_article_copyright_holder(tree)
if supplementary_materials:
result['supplementary-materials'] = _get_supplementary_materials(tree)
yield result
示例8: extract_positions
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def extract_positions(url):
tree = ElementTree()
parser = XMLParser(encoding="iso-8859-1")
data = urllib.urlopen(url)
tree.parse(data, parser=parser)
positions = tree.getroot().findall("team")
allpos = []
for pos in positions:
realpos = pos.find("pos")
latitude = float(realpos.attrib['a'])
longitude = float(realpos.attrib['o'])
speed = float(realpos.attrib['s'])
course = float(realpos.attrib['c'])
last_update = datetime.utcfromtimestamp(int(realpos.attrib["w"]))
dtf = float(realpos.attrib['d'])
_id = pos.attrib["id"]
# pos = geo.xyz(latitude, latitude)
# final object
result = {}
result["str_latitude"] = format_deg(latitude, "N", "S")
result["str_longitude"] = format_deg(longitude, "E", "W")
result["speed"] = speed
result["course"] = course
result["_id"] = _id
result["dtf"] = dtf
result["last_update"] = last_update
allpos.append(result)
return allpos
示例9: fulltext_search
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def fulltext_search(self, query, rows=None, start=None):
"""Does an advanced search on fulltext:blah.
You get back a pair (x,y) where x is the total # of hits
and y is a list of identifiers like ["foo", "bar", etc.]"""
query = self._prefix_query('fulltext', query)
result_list = self.raw_search(query, rows=rows, start=start)
e = ElementTree()
try:
e.parse(StringIO(result_list))
except SyntaxError as e:
raise SolrError(e)
total_nbr_text = e.find('info/range_info/total_nbr').text
# total_nbr_text = e.find('result').get('numFound') # for raw xml
total_nbr = int(total_nbr_text) if total_nbr_text else 0
out = []
for r in e.getiterator('hit'):
for d in r.find('metadata'):
for x in list(d.getiterator()):
if x.tag == "identifier":
xid = six.text_type(x.text).encode('utf-8')
if xid.startswith('OCA/'):
xid = xid[4:]
elif xid.endswith('.txt'):
xid = xid.split('/')[-1].split('_')[0]
elif xid.endswith('_ZZ'):
xid = xid[:-3]
out.append(xid)
break
return (total_nbr, out)
示例10: search
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def search(query):
query = query.lower()
parsed = expr.parseString(query)[0]
res = parsed()
print
print("Searching for \"{}\"".format(query))
if res[1]:
print("Found {} results".format(len(res[0])))
for file_id in list(res[0])[:5]:
print(files[file_id])
words = parsed.words()
tree = ElementTree()
tree.parse(files[file_id])
body = tree.find('text').find('body')
for p in body:
words_copy = list(words)
at_least_one = False
for word in words_copy:
if word in word_reg_exp.findall(p.text.lower().encode('utf-8')):
at_least_one = True
words.remove(word)
if at_least_one:
print("----| {}".format(p.text.encode('utf-8')))
print
else:
print("Negative requests are not allowed")
print
示例11: scrape_pkg_uri
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def scrape_pkg_uri(self, uri, pkg):
"""
Scrape package metadata from PyPi when it's running
as the Clue Release Manager.
Parameters
----------
uri : `str`
URI to page containing package's homepage
pkg : `str`
Package name
"""
# Example entry:
#<div class="distro-block distro-metadata">
# <h4>Metadata</h4>
# <dl>
# <dt>Distro Index Owner:</dt>
# <dd>acmepypi</dd>
# <dt>Home Page:</dt>
# <dd><a href="http://mysvn/acme.helloworld">
# http://mysvn/acme.helloworld</a></dd>
# </dl>
#</div>
tree = ElementTree()
try:
tree.parse(urllib2.urlopen(uri))
except urllib2.HTTPError, e:
raise UserError("Can't find repository URL for package %s (%s). "
"Has it been registered in PyPi?" % (pkg, e))
示例12: LastParser
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
class LastParser(object):
RSS_URL = "http://ws.audioscrobbler.com/2.0/user/{0}/recenttracks.rss"
def __init__(self, user):
self.tree = ElementTree()
self.tree.parse(urllib2.urlopen(self.RSS_URL.format(user)))
def get_songs(self, count=10):
l = []
for item in self.tree.getiterator("item"):
d = {}
for e in item:
d[e.tag] = e.text
l.append(d)
return l[:count]
def get_song(self):
return self.get_songs(1)[0]
def get_titles(self, count=10):
l = [title.text for title in self.tree.getiterator("title")]
return l[1:count + 1] # removing rss title
def get_title(self):
return self.get_titles(1)[0]
示例13: _DXML
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
class _DXML(object):
def __init__(self, path, tagmap={}, tagdefault=None, **options):
self._path, self._options = path, options
self._tree = ElementTree()
self._tree.parse(self._path)
self.verbosity, self.traceback, self.graceful = 1, False, False
self._tagmap = tagmap
self._tagdefault = self._trivial if tagdefault is None else tagdefault
def __iter__(self):
self._preiter_hook()
# Stage 1: namespaces
for o in self._xml_namespaces(): # IGNORE:E1101
yield o
# Stage 2: resources
r = self._tree.getroot()
for e in [r] + r.getchildren(): # IGNORE:E1101
try:
for o in self._tagmap.get(e.tag, self._tagdefault)(e):
yield o
except Exception, x:
self._except(Exception, x)
# Stage 3: inheritance etc.
self._postiter_hook()
示例14: add_from_file
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def add_from_file(self, filename):
'''parses xml file and stores wanted details'''
Gtk.Builder.add_from_file(self, filename)
# extract data for the extra interfaces
tree = ElementTree()
tree.parse(filename)
ele_widgets = tree.getiterator("object")
for ele_widget in ele_widgets:
name = ele_widget.attrib['id']
widget = self.get_object(name)
# populate indexes - a dictionary of widgets
self.widgets[name] = widget
# populate a reversed dictionary
self._reverse_widget_dict[widget] = name
# populate connections list
ele_signals = ele_widget.findall("signal")
connections = [
(name,
ele_signal.attrib['name'],
ele_signal.attrib['handler']) for ele_signal in ele_signals]
if connections:
self.connections.extend(connections)
ele_signals = tree.getiterator("signal")
for ele_signal in ele_signals:
self.glade_handler_dict.update(
{ele_signal.attrib["handler"]: None})
示例15: extractImages
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import parse [as 别名]
def extractImages(html):
if html is None:
return [], html
tree = ElementTree()
tree.parse(StringIO(html))
imagetags = tree.findall(".//img")
images = []
for tag in imagetags:
image = tag.get('src')
path, name = os.path.split(image)
if image not in images:
images.append(image)
tag.set('alt', name)
tag.set('title', name)
#index files for multipart storage
index = {}
for image in images:
path, name = os.path.split(image)
index[image] = '0x%08x' % binascii.crc32(name)
#update html email image tags
for tag in imagetags:
image = tag.get('src')
tag.set('src', "cid:%s" % index[image])
html = StringIO()
tree.write(html)
html.write("\n")
return [index, html.getvalue()]