本文整理汇总了Python中lxml.objectify.deannotate方法的典型用法代码示例。如果您正苦于以下问题:Python objectify.deannotate方法的具体用法?Python objectify.deannotate怎么用?Python objectify.deannotate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.objectify
的用法示例。
在下文中一共展示了objectify.deannotate方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_prefix
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def remove_prefix(fname):
"""This removes namespace prefix from all the things in the xml.
"""
from lxml import etree, objectify
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(fname, parser)
root = tree.getroot()
for elem in root.getiterator():
if not hasattr(elem.tag, 'find'):
continue
i = elem.tag.find('}')
if i >= 0:
elem.tag = elem.tag[i + 1:]
objectify.deannotate(root, cleanup_namespaces=True)
# fname_out = fname.replace('.xml', '.out.xml')
# tree.write(fname_out,
# pretty_print=True,
# xml_declaration=True,
# encoding='UTF-8')
return tree
示例2: main
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def main():
data = sys.stdin.readlines()
payload = data[0]
payload_dict = json.loads(payload)
if "response" in payload_dict and "body" in payload_dict["response"]:
body = payload_dict["response"]["body"]
try:
root = objectify.fromstring(str(body))
ns = "{http://ws.cdyne.com/}"
logging.debug("transforming")
ipe = ns + "ResolveIPResponse"
ipt = ns + "ResolveIPResult"
root.Body[ipe][ipt].City = "New York"
objectify.deannotate(root.Body[ipe][ipt].City)
etree.cleanup_namespaces(root.Body[ipe][ipt].City)
payload_dict["response"]["body"] = etree.tostring(root)
logging.debug(etree.tostring(root))
except Exception:
pass
print(json.dumps(payload_dict))
示例3: rich_title
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def rich_title(self):
"""For an individual PLOS article, get its title with HTML formatting.
Preserves HTML formatting but removes all other XML tagging, namespace/xlink info, etc.
Doesn't do xpath directly on `self.root` so can deannotate separate object
See http://lxml.de/objectify.html#how-data-types-are-matched for more info on deannotate process
Exceptions that still need handling:
10.1371/journal.pone.0179720, 10.1371/journal.pone.0068479, 10.1371/journal.pone.0069681,
10.1371/journal.pone.0068965, 10.1371/journal.pone.0083868, 10.1371/journal.pone.0069554,
10.1371/journal.pone.0068324, 10.1371/journal.pone.0067986, 10.1371/journal.pone.0068704,
10.1371/journal.pone.0068492, 10.1371/journal.pone.0068764, 10.1371/journal.pone.0068979,
10.1371/journal.pone.0068544, 10.1371/journal.pone.0069084, 10.1371/journal.pone.0069675
:return: string of article title at specified xpath location
"""
root = self.root
objectify.deannotate(root, cleanup_namespaces=True, xsi_nil=True)
art_title = root.xpath("/article/front/article-meta/title-group/article-title")
art_title = art_title[0]
try:
text = art_title.text
if text is None:
text = ''
text += ''.join(et.tostring(child, encoding='unicode') if child.tag not in ('ext-link', 'named-content', 'sc', 'monospace') \
else child.text + child.tail if child.tail is not None \
else child.text
for child in art_title.getchildren())
title = text.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"', '') \
.replace(' xmlns:mml="http://www.w3.org/1998/Math/MathML"', '') \
.replace(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance', '')
except TypeError:
# try to rewrite so this isn't needed
print('Error processing article title for {}'.format(self.doi))
title = et.tostring(art_title, method='text', encoding='unicode')
return title
示例4: _save_script
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def _save_script(self):
# objectify.deannotate(self.dragon)
etree.cleanup_namespaces(self.dragon)
self.drs_name = 'Dragon_script.drs'
with open(os.path.join(self.output_directory, self.drs_name), 'w') as outfile:
outfile.write(etree.tostring(self.dragon, pretty_print=True).decode())
示例5: printout
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def printout(self):
objectify.deannotate(self.dragon)
etree.cleanup_namespaces(self.dragon)
print(objectify.dump(self.dragon))
示例6: xpath
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def xpath(source_xml, xpath_expr, req_format='string'):
""" Filter xml based on an xpath expression.
Purpose: This function applies an Xpath expression to the XML
| supplied by source_xml. Returns a string subtree or
| subtrees that match the Xpath expression. It can also return
| an xml object if desired.
@param source_xml: Plain text XML that will be filtered
@type source_xml: str or lxml.etree.ElementTree.Element object
@param xpath_expr: Xpath expression that we will filter the XML by.
@type xpath_expr: str
@param req_format: the desired format of the response, accepts string or
| xml.
@type req_format: str
@returns: The filtered XML if filtering was successful. Otherwise,
| an empty string.
@rtype: str or ElementTree
"""
tree = source_xml
if not isinstance(source_xml, ET.Element):
tree = objectify.fromstring(source_xml)
# clean up the namespace in the tags, as namespaces appear to confuse
# xpath method
for elem in tree.getiterator():
# beware of factory functions such as Comment
if isinstance(elem.tag, basestring):
i = elem.tag.find('}')
if i >= 0:
elem.tag = elem.tag[i+1:]
# remove unused namespaces
objectify.deannotate(tree, cleanup_namespaces=True)
filtered_list = tree.xpath(xpath_expr)
# Return string from the list of Elements or pure xml
if req_format == 'xml':
return filtered_list
matches = ''.join(etree.tostring(
element, pretty_print=True) for element in filtered_list)
return matches if matches else ""
示例7: cleanup_namespace
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def cleanup_namespace(root):
# http://stackoverflow.com/a/18160164
for elem in root.getiterator():
if not hasattr(elem.tag, 'find'):
continue
i = elem.tag.find('}')
if i >= 0:
elem.tag = elem.tag[i + 1:]
objectify.deannotate(root, cleanup_namespaces=True)
示例8: __getitem__
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def __getitem__(self, key):
"""Return the value for attr key."""
attr = getattr(self.root, key, None)
objectify.deannotate(self.root)
return attr
示例9: __setitem__
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def __setitem__(self, key, value):
"""Set value for attr key."""
_prev = getattr(self.root, 'modelVersion', None)
if key in ('groupId', 'artifactId', 'name', 'version', 'packaging') and _prev is not None:
# add these tags just after modelVersion tag.
element = etree.Element(key)
element.text = value
_prev.addnext(element)
else:
setattr(self.root, key, value)
objectify.deannotate(self.root)
self._reload(self.root)
示例10: tostring
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def tostring(obj, decoding=False):
"""Convert the xml object into string.
:returns: String
"""
if getattr(obj, '_commit', None) is not None:
obj._commit()
objectify.deannotate(obj.root, xsi_nil=True,
pytype=False, xsi=False, cleanup_namespaces=True)
_str = etree.tostring(obj.root, pretty_print=True)
if decoding:
return _str.decode()
return _str
示例11: _reload
# 需要导入模块: from lxml import objectify [as 别名]
# 或者: from lxml.objectify import deannotate [as 别名]
def _reload(obj):
obj = objectify.fromstring(etree.tostring(obj))
objectify.deannotate(obj, xsi_nil=True, cleanup_namespaces=True)
return obj