本文整理汇总了Python中lxml.etree.ElementTree.fromstring方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.fromstring方法的具体用法?Python ElementTree.fromstring怎么用?Python ElementTree.fromstring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.fromstring方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_connect
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import fromstring [as 别名]
def test_connect(self):
server = V1Server(instance_url=INSTANCE_URL, token=TOKEN)
code, body = server.fetch('/rest-1.v1/Data/Story?sel=Name')
# self.assertEquals(200, code)
# print "\n\nCode: ", code
# print "Body: ", body
elem = ElementTree.fromstring(body)
self.assertEquals(elem.tag, 'Assets')
示例2: parse_xml
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import fromstring [as 别名]
def parse_xml(body, code, headers):
"""
Return a dictionary.
Transformation is done assuming:
1. If a tag indicates its value type, we'll try to cast it.
2. Siblings with the same tag name, become a list.
3. Attributes become a dict key starting with '@'.
<duck>
<name>Donald</name>
<birth_date type="datetime">1934-06-04T00:00:00</birth_date>
<first_film/>
<last_film></last_film>
<species href="http://en.wikipedia.org/wiki/Pekin_duck"/>
<created_by href="http://en.wikipedia.org/wiki/Walt_Disney">
<name>Walt Disney</name>
<cryopreserved type="boolean">true</cryopreserved>
</created_by>
<family>
<children type="array"></children>
<uncles type="array">
<uncle><name>Scrooge McDuck</name></uncle>
<uncle><name>Ludwig Von Drake</name></uncle>
</uncles>
<nephew><name>Huey</name></nephew>
<nephew><name>Dewey</name></nephew>
<nephew><name>Louie</name></nephew>
</family>
</duck>
{'duck': {'birth_date': '1934-06-04T00:00:00',
'created_by': {'@href': 'http://en.wikipedia.org/wiki/Walt_Disney',
'cryopreserved': True,
'name': 'Walt Disney'},
'family': {'nephew': [{'name': 'Huey'},
{'name': 'Dewey'},
{'name': 'Louie'}],
'children': [],
'uncles': {'uncle': [{'name': 'Scrooge McDuck'},
{'name': 'Ludwig Von Drake'}]}},
'first_film': None,
'last_film': None,
'name': 'Donald',
'species': {'@href': 'http://en.wikipedia.org/wiki/Pekin_duck'}}
}
"""
if not 200 <= code < 300:
raise http.HTTPError(body, code, headers)
root_elem = etree.fromstring(body)
return {
root_elem.tag: value_for_element(root_elem)
}
示例3: get_xml
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import fromstring [as 别名]
def get_xml(self, path, query="", postdata=None):
verb = "HTTP POST to " if postdata else "HTTP GET from "
msg = verb + path
self.logger.info(msg)
exception, body = self.fetch(path, query=query, postdata=postdata)
if exception:
self.handle_non_xml_response(body, exception, msg, postdata)
self.logger.warn("{0} during {1}".format(exception, msg))
if postdata is not None:
self.logger.warn(postdata)
document = ElementTree.fromstring(body)
if exception:
exception.xmldoc = document
if exception.code == 404:
raise V1AssetNotFoundError(exception)
elif exception.code == 400:
raise V1Error("\n" + body)
else:
raise V1Error(exception)
return document