本文整理汇总了Python中xml.etree.cElementTree.ElementTree.iter方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.iter方法的具体用法?Python ElementTree.iter怎么用?Python ElementTree.iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.cElementTree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.iter方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_article_contrib_authors
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def _get_article_contrib_authors(tree):
from sys import stderr
"""
Given an ElementTree, returns article authors in a format suitable for citation.
"""
authors = []
front = ElementTree(tree).find('front')
for contrib in front.iter('contrib'):
contribTree = ElementTree(contrib)
try:
surname = contribTree.find('name/surname').text
except AttributeError: # author is not a natural person
try:
citation_name = contribTree.find('collab').text
if citation_name is not None:
authors.append(citation_name)
continue
except AttributeError: # name has no immediate text node
continue
try:
given_names = contribTree.find('name/given-names').text
citation_name = ' '.join([surname, given_names[0]])
except AttributeError: # no given names
citation_name = surname
except TypeError: # also no given names
citation_name = surname
if citation_name is not None:
authors.append(citation_name)
return ', '.join(authors)
示例2: _get_pmcid
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def _get_pmcid(tree):
"""
Given an ElementTree, returns PubMed Central ID.
"""
front = ElementTree(tree).find('front')
for article_id in front.iter('article-id'):
if article_id.attrib['pub-id-type'] == 'pmc':
return article_id.text
示例3: _get_journal_title
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def _get_journal_title(tree):
"""
Given an ElementTree, returns journal title.
"""
front = ElementTree(tree).find('front')
for journal_meta in front.iter('journal-meta'):
for journal_title in journal_meta.iter('journal-title'):
return journal_title.text
示例4: _get_article_url
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def _get_article_url(tree):
"""
Given an ElementTree, returns article URL.
"""
article_meta = ElementTree(tree).find('front/article-meta')
for article_id in article_meta.iter('article-id'):
if article_id.attrib['pub-id-type'] == 'doi':
return 'http://dx.doi.org/' + article_id.text
return '' # FIXME: this should never, ever happen
示例5: parse_xml
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def parse_xml(self, path):
pwd_found = []
if os.path.exists(path):
tree = ElementTree(file=path)
elements = {'name': 'Name', 'url': 'URL', 'userName': 'Login', 'password': 'Password'}
for elem in tree.iter('Bean'):
values = {}
for e in elem:
if e.tag in elements:
values[elements[e.tag]] = e.text
if values:
pwd_found.append(values)
return pwd_found
示例6: run
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def run(self):
path = os.path.join(constant.profile['USERPROFILE'], u'.squirrel-sql', u'SQLAliases23.xml')
if os.path.exists(path):
tree = ElementTree(file=path)
pwd_found = []
elements = {'name': 'Name', 'url': 'URL', 'userName': 'Login', 'password': 'Password'}
for elem in tree.iter('Bean'):
values = {}
for e in elem:
if e.tag in elements:
values[elements[e.tag]] = e.text
if values:
pwd_found.append(values)
return pwd_found
示例7: _get_article_date
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def _get_article_date(tree):
"""
Given an ElementTree, returns article date.
"""
article_meta = ElementTree(tree).find('front/article-meta')
for pub_date in article_meta.iter('pub-date'):
if pub_date.attrib['pub-type'] == 'epub':
pub_date_tree = ElementTree(pub_date)
year = int(pub_date_tree.find('year').text)
try:
month = int(pub_date_tree.find('month').text)
except AttributeError:
month = 1 # TODO: is this correct?
try:
day = int(pub_date_tree.find('day').text)
except AttributeError:
day = 1 # TODO: is this correct?
return str(date(year, month, day))
return ''
示例8: run
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def run(self):
xml_file = self.get_application_path()
if xml_file and os.path.exists(xml_file):
tree = ElementTree(file=xml_file)
pwd_found = []
for elem in tree.iter():
try:
if elem.attrib['name'].startswith('ftp') or elem.attrib['name'].startswith('ftps') \
or elem.attrib['name'].startswith('sftp') or elem.attrib['name'].startswith('http') \
or elem.attrib['name'].startswith('https'):
encrypted_password = base64.b64decode(elem.attrib['value'])
password = Win32CryptUnprotectData(encrypted_password, is_current_user=constant.is_current_user, user_dpapi=constant.user_dpapi)
pwd_found.append({
'URL': elem.attrib['name'],
'Password': password,
})
except Exception as e:
self.debug(str(e))
return pwd_found
示例9: get_passphrase
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def get_passphrase(self, path):
xml_name = u'product-preferences.xml'
xml_file = None
if os.path.exists(os.path.join(path, xml_name)):
xml_file = os.path.join(path, xml_name)
else:
for p in os.listdir(path):
if p.startswith('system'):
new_directory = os.path.join(path, p)
for pp in os.listdir(new_directory):
if pp.startswith(u'o.sqldeveloper'):
if os.path.exists(os.path.join(new_directory, pp, xml_name)):
xml_file = os.path.join(new_directory, pp, xml_name)
break
if xml_file:
tree = ElementTree(file=xml_file)
for elem in tree.iter():
if 'n' in elem.attrib.keys():
if elem.attrib['n'] == 'db.system.id':
return elem.attrib['v']
示例10: create_db
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
def create_db(file_path):
tree = ElementTree(file=file_path)
root = tree.getroot()
db_name = root.attrib['name']
conn = sqlite3.connect(db_name)
c = conn.cursor()
for table in tree.iter(tag='table'):
create_sql_arr = ['CREATE TABLE IF NOT EXISTS %s' % table.attrib['name'], ' (']
index = 0
for column in table:
if index != 0:
create_sql_arr.append(', ')
index += 1
create_sql_arr.append('%s %s' % (column.attrib['name'], column.attrib['type']))
if 'pk' in column.attrib and column.attrib['pk'] == '1':
create_sql_arr.append(' PRIMARY KEY')
create_sql_arr.append(')')
create_sql = ''.join(create_sql_arr)
c.execute(create_sql)
print 'Execute sql:%s' % create_sql
conn.commit()
conn.close()
示例11: fail
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import iter [as 别名]
__copyright__ = 'Copyright (C) 2015 Karl Wette'
import sys
from xml.etree.cElementTree import ElementTree
# print error message and exit
def fail(msg):
sys.stderr.write('%s: %s\n' % (sys.argv[0], msg))
sys.exit(1)
# get input arguments
tagfile, = sys.argv[1:]
# parse tag file
tree = ElementTree()
try:
tree.parse(tagfile)
except:
fail("could not parse XML input from '%s'" % tagfile)
# warn on duplicate documentation anchors
anchors = dict()
for elem in tree.iter('docanchor'):
if elem.text in anchors:
anchors[elem.text] = anchors[elem.text] + 1
else:
anchors[elem.text] = 1
for anchor in anchors:
if anchors[anchor] > 1:
print('%s: warning: duplicate anchor %s' % (tagfile, anchor))