本文整理汇总了Python中cElementTree.fromstring方法的典型用法代码示例。如果您正苦于以下问题:Python cElementTree.fromstring方法的具体用法?Python cElementTree.fromstring怎么用?Python cElementTree.fromstring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cElementTree
的用法示例。
在下文中一共展示了cElementTree.fromstring方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CreateClassFromXMLString
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def CreateClassFromXMLString(target_class, xml_string, string_encoding=None):
"""Creates an instance of the target class from the string contents.
Args:
target_class: class The class which will be instantiated and populated
with the contents of the XML. This class must have a _tag and a
_namespace class variable.
xml_string: str A string which contains valid XML. The root element
of the XML string should match the tag and namespace of the desired
class.
string_encoding: str The character encoding which the xml_string should
be converted to before it is interpreted and translated into
objects. The default is None in which case the string encoding
is not changed.
Returns:
An instance of the target class with members assigned according to the
contents of the XML - or None if the root XML tag and namespace did not
match those of the target class.
"""
encoding = string_encoding or XML_STRING_ENCODING
if encoding and isinstance(xml_string, unicode):
xml_string = xml_string.encode(encoding)
tree = ElementTree.fromstring(xml_string)
return _CreateClassFromElementTree(target_class, tree)
示例2: parse
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def parse(xml_string, target_class=None, version=1, encoding=None):
"""Parses the XML string according to the rules for the target_class.
Args:
xml_string: str or unicode
target_class: XmlElement or a subclass. If None is specified, the
XmlElement class is used.
version: int (optional) The version of the schema which should be used when
converting the XML into an object. The default is 1.
encoding: str (optional) The character encoding of the bytes in the
xml_string. Default is 'UTF-8'.
"""
if target_class is None:
target_class = XmlElement
if isinstance(xml_string, unicode):
if encoding is None:
xml_string = xml_string.encode(STRING_ENCODING)
else:
xml_string = xml_string.encode(encoding)
tree = ElementTree.fromstring(xml_string)
return _xml_element_from_tree(tree, target_class, version)
示例3: _le_xml
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def _le_xml(self, arquivo):
if arquivo is None:
return False
if not isinstance(arquivo, basestring):
arquivo = etree.tounicode(arquivo)
if arquivo is not None:
if isinstance(arquivo, basestring):
if NAMESPACE_NFSE in arquivo:
arquivo = por_acentos(arquivo)
if u'<' in arquivo:
self._xml = etree.fromstring(tira_abertura(arquivo))
else:
arq = open(arquivo)
txt = ''.join(arq.readlines())
txt = tira_abertura(txt)
arq.close()
self._xml = etree.fromstring(txt)
else:
self._xml = etree.parse(arquivo)
return True
return False
示例4: validar
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def validar(self):
arquivo_esquema = self.caminho_esquema + self.arquivo_esquema
# Aqui é importante remover a declaração do encoding
# para evitar erros de conversão unicode para ascii
xml = tira_abertura(self.xml).encode(u'utf-8')
esquema = etree.XMLSchema(etree.parse(arquivo_esquema))
if not esquema.validate(etree.fromstring(xml)):
for e in esquema.error_log:
if e.level == 1:
self.alertas.append(e.message.replace('{http://www.portalfiscal.inf.br/nfe}', ''))
elif e.level == 2:
self.erros.append(e.message.replace('{http://www.portalfiscal.inf.br/nfe}', ''))
return esquema.error_log
示例5: extract_root_elem
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def extract_root_elem(xml_str):
"""
extracts root xml element from xml string.
Args:
xml_str (str): xml string
Returns:
xml element
Example:
xml_str='''
<lsClone dn="org-root/ls-testsp" inHierarchical="false"
inServerName="test" inTargetOrg="">
</lsClone>
'''
root_element = extract_root_elem(xml_str)
"""
xml_str = xml_str.strip("\x00")
root_elem = ET.fromstring(xml_str)
return root_elem
示例6: CreateClassFromXMLString
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def CreateClassFromXMLString(target_class, xml_string, string_encoding=None):
"""Creates an instance of the target class from the string contents.
Args:
target_class: class The class which will be instantiated and populated
with the contents of the XML. This class must have a _tag and a
_namespace class variable.
xml_string: str A string which contains valid XML. The root element
of the XML string should match the tag and namespace of the desired
class.
string_encoding: str The character encoding which the xml_string should
be converted to before it is interpreted and translated into
objects. The default is None in which case the string encoding
is not changed.
Returns:
An instance of the target class with members assigned according to the
contents of the XML - or None if the root XML tag and namespace did not
match those of the target class.
"""
encoding = string_encoding or XML_STRING_ENCODING
if encoding and isinstance(xml_string, unicode):
xml_string = xml_string.encode(encoding)
tree = ElementTree.fromstring(xml_string)
return _CreateClassFromElementTree(target_class, tree)
示例7: extract_root_elem
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def extract_root_elem(xml_str):
"""
extracts root xml element from xml string.
Args:
xml_str (str): xml string
Returns:
xml element
Example:
xml_str='''
<lsClone dn="org-root/ls-testsp" inHierarchical="false"
inServerName="test" inTargetOrg="">
</lsClone>
'''
root_element = extract_root_elem(xml_str)
"""
root_elem = ET.fromstring(xml_str)
return root_elem
示例8: __init__
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def __init__(self, response):
Error.__init__(self, response)
try:
self.element_tree = ElementTree.fromstring(response['body'])
self.error_code = int(self.element_tree[0].attrib['errorCode'])
self.reason = self.element_tree[0].attrib['reason']
self.invalidInput = self.element_tree[0].attrib['invalidInput']
except:
self.error_code = 600
示例9: ExtensionElementFromString
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def ExtensionElementFromString(xml_string):
element_tree = ElementTree.fromstring(xml_string)
return _ExtensionElementFromElementTree(element_tree)
示例10: from_xml_str
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def from_xml_str(xml_str, handle=None):
"""
Generates response object from the given xml string.
Args:
xml_str (str): xml string
handle : ImcHandle
Returns:
object (external method or managed object or generic managed object)
Example:
xml_str='''\n
<lsServer dn="org-root/ls-testsp" dynamicConPolicyName="test"\n
extIPPoolName="ext-mgmt" name="testsp" />\n
'''\n
root_element = extract_root_elem(xml_str)\n
"""
xml_str = xml_str.strip("\x00")
root_elem = ET.fromstring(xml_str)
if root_elem.tag == "error":
error_code = root_elem.attrib['errorCode']
error_descr = root_elem.attrib['errorDescr']
raise ex.ImcException(error_code, error_descr)
class_id = imcgenutils.word_u(root_elem.tag)
response = imccoreutils.get_imc_obj(class_id, root_elem)
response.from_xml(root_elem, handle)
return response
示例11: generic_mo_from_xml
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def generic_mo_from_xml(xml_str):
"""
create GenericMo object from xml string
"""
root_elem = ET.fromstring(xml_str)
class_id = root_elem.tag
gmo = GenericMo(class_id)
gmo.from_xml(root_elem)
return gmo
示例12: prettify
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def prettify(self, elem):
"""
Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf8')
root = etree.fromstring(rough_string)
try:
return etree.tostring(root, pretty_print=True)
except TypeError:
return etree.tostring(root)
示例13: GetFormUploadToken
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def GetFormUploadToken(self, video_entry, uri=YOUTUBE_UPLOAD_TOKEN_URI):
"""Receives a YouTube Token and a YouTube PostUrl from a YouTubeVideoEntry.
Needs authentication.
Args:
video_entry: The YouTubeVideoEntry to upload (meta-data only).
uri: An optional string representing the URI from where to fetch the
token information. Defaults to the YOUTUBE_UPLOADTOKEN_URI.
Returns:
A tuple containing the URL to which to post your video file, along
with the youtube token that must be included with your upload in the
form of: (post_url, youtube_token).
"""
try:
response = self.Post(video_entry, uri)
except gdata.service.RequestError, e:
raise YouTubeError(e.args[0])
tree = ElementTree.fromstring(response)
for child in tree:
if child.tag == 'url':
post_url = child.text
elif child.tag == 'token':
youtube_token = child.text
return (post_url, youtube_token)
示例14: xml_element_from_string
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def xml_element_from_string(xml_string, target_class,
version=1, encoding='UTF-8'):
"""Parses the XML string according to the rules for the target_class.
Args:
xml_string: str or unicode
target_class: XmlElement or a subclass.
version: int (optional) The version of the schema which should be used when
converting the XML into an object. The default is 1.
"""
tree = ElementTree.fromstring(xml_string)
return _xml_element_from_tree(tree, target_class, version)
示例15: from_xml_str
# 需要导入模块: import cElementTree [as 别名]
# 或者: from cElementTree import fromstring [as 别名]
def from_xml_str(xml_str, handle=None):
"""
Generates response object from the given xml string.
Args:
xml_str (str): xml string
Returns:
object (external method or managed object or generic managed object)
Example:
xml_str='''\n
<lsServer dn="org-root/ls-testsp" dynamicConPolicyName="test"\n
extIPPoolName="ext-mgmt" name="testsp" />\n
'''\n
root_element = extract_root_elem(xml_str)\n
"""
root_elem = ET.fromstring(xml_str)
if root_elem.tag == "error":
error_code = root_elem.attrib['errorCode']
error_descr = root_elem.attrib['errorDescr']
raise ex.UcscException(error_code, error_descr)
class_id = ucscgenutils.word_u(root_elem.tag)
response = ucsccoreutils.get_ucsc_obj(class_id, root_elem)
response.from_xml(root_elem, handle)
return response