本文整理汇总了Python中lxml.etree.XML类的典型用法代码示例。如果您正苦于以下问题:Python XML类的具体用法?Python XML怎么用?Python XML使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了XML类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validateFootnote
def validateFootnote(modelXbrl, footnote):
#handler = TextBlockHandler(modelXbrl)
loadDTD(modelXbrl)
checkedGraphicsFiles = set() # only check any graphics file reference once per footnote
try:
footnoteHtml = XML("<body/>")
copyHtml(footnote, footnoteHtml)
if not edbodyDTD.validate( footnoteHtml ):
modelXbrl.error("EFM.6.05.34.dtdError",
_("Footnote %(xlinkLabel)s causes the XML error %(error)s"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
error=', '.join(e.message for e in edbodyDTD.error_log.filter_from_errors()))
for elt in footnoteHtml.iter():
eltTag = elt.tag
for attrTag, attrValue in elt.items():
if ((attrTag == "href" and eltTag == "a") or
(attrTag == "src" and eltTag == "img")):
if "javascript:" in attrValue:
modelXbrl.error("EFM.6.05.34.activeContent",
_("Footnote %(xlinkLabel)s has javascript in '%(attribute)s' for <%(element)s>"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrTag, element=eltTag)
elif attrValue.startswith("http://www.sec.gov/Archives/edgar/data/") and eltTag == "a":
pass
elif "http:" in attrValue or "https:" in attrValue or "ftp:" in attrValue:
modelXbrl.error("EFM.6.05.34.externalReference",
_("Footnote %(xlinkLabel)s has an invalid external reference in '%(attribute)s' for <%(element)s>: %(value)s"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrTag, element=eltTag, value=attrValue)
if attrTag == "src" and attrValue not in checkedGraphicsFiles:
if attrValue.lower()[-4:] not in ('.jpg', '.gif'):
modelXbrl.error("EFM.6.05.34.graphicFileType",
_("Footnote %(xlinkLabel)s references a graphics file which isn't .gif or .jpg '%(attribute)s' for <%(element)s>"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrValue, element=eltTag)
else: # test file contents
try:
if validateGraphicFile(footnote, attrValue) != attrValue.lower()[-3:]:
modelXbrl.error("EFM.6.05.34.graphicFileContent",
_("Footnote %(xlinkLabel)s references a graphics file which doesn't have expected content '%(attribute)s' for <%(element)s>"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrValue, element=eltTag)
except IOError as err:
modelXbrl.error("EFM.6.05.34.graphicFileError",
_("Footnote %(xlinkLabel)s references a graphics file which isn't openable '%(attribute)s' for <%(element)s>, error: %(error)s"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrValue, element=eltTag, error=err)
checkedGraphicsFiles.add(attrValue)
if eltTag == "table" and any(a is not None for a in elt.iterancestors("table")):
modelXbrl.error("EFM.6.05.34.nestedTable",
_("Footnote %(xlinkLabel)s has nested <table> elements."),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"))
except (XMLSyntaxError,
UnicodeDecodeError) as err:
#if not err.endswith("undefined entity"):
modelXbrl.error("EFM.6.05.34",
_("Footnote %(xlinkLabel)s causes the XML error %(error)s"),
modelObject=footnote, xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
error=edbodyDTD.error_log.filter_from_errors())
示例2: do_search
def do_search(param, sort, page=1, rows=100):
(reply, solr_select, q_list) = run_solr_query(param, rows, page, sort)
is_bad = False
if reply.startswith('<html'):
is_bad = True
if not is_bad:
try:
root = XML(reply)
except XMLSyntaxError:
is_bad = True
if is_bad:
m = re_pre.search(reply)
return web.storage(
facet_counts = None,
docs = [],
is_advanced = bool(param.get('q', 'None')),
num_found = None,
solr_select = solr_select,
q_list = q_list,
error = (web.htmlunquote(m.group(1)) if m else reply),
)
docs = root.find('result')
return web.storage(
facet_counts = read_facets(root),
docs = docs,
is_advanced = bool(param.get('q', 'None')),
num_found = (int(docs.attrib['numFound']) if docs is not None else None),
solr_select = solr_select,
q_list = q_list,
error = None,
)
示例3: get_article_by_search_wap
def get_article_by_search_wap(keyword, wap_dict):
datas = []
for i in wap_dict['items']:
item = str_to_bytes(i).replace(b'\xee\x90\x8a' + str_to_bytes(keyword) + b'\xee\x90\x8b',
str_to_bytes(keyword))
root = XML(item)
display = root.find('.//display')
datas.append({
'gzh': {
'profile_url': display.find('encGzhUrl').text,
'open_id': display.find('openid').text,
'isv': display.find('isV').text,
'wechat_name': display.find('sourcename').text,
'wechat_id': display.find('username').text,
'headimage': display.find('headimage').text,
'qrcode': display.find('encQrcodeUrl').text,
},
'article': {
'title': display.find('title').text,
'url': display.find('url').text, # encArticleUrl
'main_img': display.find('imglink').text,
'abstract': display.find('content168').text,
'time': display.find('lastModified').text,
},
})
return datas
示例4: iter_transactions
def iter_transactions(self, coming):
url = self.get_part_url()
if url is None:
# There are no transactions in this kind of account
return
while True:
d = XML(self.browser.readurl(url))
try:
el = self.parser.select(d, '//dataBody', 1, 'xpath')
except BrokenPageError:
# No transactions.
return
s = StringIO(unicode(el.text).encode('iso-8859-1'))
doc = self.browser.get_document(s)
for tr in self._iter_transactions(doc, coming):
if not tr._coming:
coming = False
yield tr
el = d.xpath('//dataHeader')[0]
if int(el.find('suite').text) != 1:
return
url = urlparse(url)
p = parse_qs(url.query)
url = self.browser.buildurl(url.path, n10_nrowcolor=0,
operationNumberPG=el.find('operationNumber').text,
operationTypePG=el.find('operationType').text,
pageNumberPG=el.find('pageNumber').text,
idecrit=el.find('idecrit').text or '',
sign=p['sign'][0],
src=p['src'][0])
示例5: RefSeq
class RefSeq(object):
def __init__(self,xml):
self._root = XML(xml)
def cds_start_end(self):
n = self._root.xpath('/GBSet/GBSeq/GBSeq_feature-table/GBFeature[GBFeature_key/text()="CDS"]')
assert len(n) == 1, "expected exactly one CDS GBFeature_key node"
s,e = _feature_se(n[0])
return s,e
def cds_start(self):
return self.cds_start_end()[0]
def chr(self):
return self._root.xpath('/GBSet/GBSeq/GBSeq_feature-table/GBFeature['
'GBFeature_key/text()="source"]/GBFeature_quals'
'/GBQualifier[GBQualifier_name/text()='
'"chromosome"]/GBQualifier_value')[0].text
def exons(self):
exon_nodes = self._root.xpath('/GBSet/GBSeq/GBSeq_feature-table/GBFeature[GBFeature_key="exon"]')
return [ _feature_se(n) for n in exon_nodes ]
def seq(self):
return self._root.xpath('/GBSet/GBSeq/GBSeq_sequence')[0].text
示例6: parse_gir
def parse_gir(gir_path):
"""Extract everything from a gir file"""
print("Parsing {}".format(gir_path))
parser = XMLParser(encoding="utf-8", recover=True)
content = open(gir_path).read()
root = XML(content, parser)
namespace = root.findall("{%s}namespace" % XMLNS)[0]
namespace_content = extract_namespace(namespace)
return namespace_content
示例7: Index
def Index(self):
'''Build local data structures'''
try:
xdata = XML(self.data)
except XMLSyntaxError:
logger.error("Failed to parse %s"%(self.name))
return
self.label = xdata.attrib[self.__identifier__]
self.entries = xdata.getchildren()
示例8: iter_transactions
def iter_transactions(self):
url = self.get_part_url()
if url is None:
# There are no transactions in this kind of account
return
is_deferred_card = bool(self.doc.xpath(u'//div[contains(text(), "Différé")]'))
has_summary = False
if is_deferred_card:
coming_debit_date = None
# get coming debit date for deferred_card
date_string = Regexp(CleanText(u'//option[contains(text(), "détail des factures à débiter le")]'),
r'(\d{2}/\d{2}/\d{4})',
default=NotAvailable)(self.doc)
if date_string:
coming_debit_date = parse_d(date_string)
while True:
d = XML(self.browser.open(url).content)
el = d.xpath('//dataBody')
if not el:
return
el = el[0]
s = unicode(el.text).encode('iso-8859-1')
doc = fromstring(s)
for tr in self._iter_transactions(doc):
if tr.type == Transaction.TYPE_CARD_SUMMARY:
has_summary = True
if is_deferred_card and tr.type is Transaction.TYPE_CARD:
tr.type = Transaction.TYPE_DEFERRED_CARD
if not has_summary:
if coming_debit_date:
tr.date = coming_debit_date
tr._coming = True
yield tr
el = d.xpath('//dataHeader')[0]
if int(el.find('suite').text) != 1:
return
url = urlparse(url)
p = parse_qs(url.query)
args = {}
args['n10_nrowcolor'] = 0
args['operationNumberPG'] = el.find('operationNumber').text
args['operationTypePG'] = el.find('operationType').text
args['pageNumberPG'] = el.find('pageNumber').text
args['idecrit'] = el.find('idecrit').text or ''
args['sign'] = p['sign'][0]
args['src'] = p['src'][0]
url = '%s?%s' % (url.path, urlencode(args))
示例9: parse_result
def parse_result( self, string, bs=False ):
root = XML(string)
sents = root.xpath(".//sentence")
if not bs:
return list(
itertools.chain.from_iterable( re.findall("\u3000([^(]+)\(\w+\)""",sent.text) for sent in sents )
)
else:
return list(
( re.findall("\u3000([^(]+)\(\w+\)""",sent.text) for sent in sents )
)
示例10: process_update
def process_update(self, xml):
messages = []
xml = XML(xml)
msgs = xml.findall("log/msg")
for msg in msgs:
author = msg.findtext("author")
text = msg.findtext("text")
messages.append(ChatMessage(author, text))
self.messages = messages
示例11: validateFootnote
def validateFootnote(modelXbrl, footnote):
# handler = TextBlockHandler(modelXbrl)
loadDTD(modelXbrl)
try:
footnoteHtml = XML("<body/>")
copyHtml(footnote, footnoteHtml)
if not edbodyDTD.validate(footnoteHtml):
modelXbrl.error(
"EFM.6.05.34",
_("Footnote %(xlinkLabel)s causes the XML error %(error)s"),
modelObject=footnote,
xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
error=", ".join(e.message for e in edbodyDTD.error_log.filter_from_errors()),
)
for elt in footnoteHtml.iter():
eltTag = elt.tag
for attrTag, attrValue in elt.items():
if (attrTag == "href" and eltTag == "a") or (attrTag == "src" and eltTag == "img"):
if "javascript:" in attrValue:
modelXbrl.error(
"EFM.6.05.34",
_("Footnote %(xlinkLabel)s has javascript in '%(attribute)s' for <%(element)s>"),
modelObject=footnote,
xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrTag,
element=eltTag,
)
elif attrValue.startswith("http://www.sec.gov/Archives/edgar/data/") and eltTag == "a":
pass
elif "http:" in attrValue or "https:" in attrValue or "ftp:" in attrValue:
modelXbrl.error(
"EFM.6.05.34",
_(
"Footnote %(xlinkLabel)s has an invalid external reference in '%(attribute)s' for <%(element)s>: %(value)s"
),
modelObject=footnote,
xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
attribute=attrTag,
element=eltTag,
value=attrValue,
)
except (XMLSyntaxError, UnicodeDecodeError) as err:
# if not err.endswith("undefined entity"):
modelXbrl.error(
"EFM.6.05.34",
_("Footnote %(xlinkLabel)s causes the XML error %(error)s"),
modelObject=footnote,
xlinkLabel=footnote.get("{http://www.w3.org/1999/xlink}label"),
error=edbodyDTD.error_log.filter_from_errors(),
)
示例12: parse_error
def parse_error(xml_str):
error = {}
doc = XML(xml_str)
if doc.tag == 'error':
elem = doc
elif doc.tag == 'customers':
elem = doc.find('.//error')
else:
raise Exception("Can't find error element in '%s'" % xml_str)
client_log.debug(elem)
error['id'] = elem.attrib['id']
error['code'] = elem.attrib['code']
error['aux_code'] = elem.attrib['auxCode']
error['message'] = elem.text
return error
示例13: parse_error
def parse_error(xml_str):
error = {}
doc = XML(xml_str)
if doc.tag == "error":
elem = doc
elif doc.tag == "customers":
elem = doc.find(".//error")
else:
raise Exception("Can't find error element in '%s'" % xml_str)
client_log.debug(elem)
error["id"] = elem.attrib["id"]
error["code"] = elem.attrib["code"]
error["aux_code"] = elem.attrib["auxCode"]
error["message"] = elem.text
return error
示例14: XMLFileBacked
class XMLFileBacked(FileBacked):
"""
This object is a coherent cache for an XML file to be used as a
part of DirectoryBacked.
"""
__identifier__ = 'name'
def __init__(self, filename):
self.label = "dummy"
self.entries = []
FileBacked.__init__(self, filename)
def Index(self):
"""Build local data structures."""
try:
self.xdata = XML(self.data)
except XMLSyntaxError:
logger.error("Failed to parse %s" % (self.name))
return
self.entries = self.xdata.getchildren()
if self.__identifier__ is not None:
self.label = self.xdata.attrib[self.__identifier__]
def __iter__(self):
return iter(self.entries)
def __str__(self):
return "%s: %s" % (self.name, lxml.etree.tostring(self.xdata))
示例15: _add_children
def _add_children(self, bus, service):
'''Add the child nodes found by introspection'''
self._children= {};
xml = self._object.Introspect()
data = XML(xml)
# add all child nodes
for child in data.findall('node'):
name = child.get('name')
if name == "/":
continue
child_path = self.object.object_path
# root is reported as /, don't make it //
if child_path != "/":
child_path += "/"
child_path += name
self._children[name] = Dbusitem(bus, service, child_path)