当前位置: 首页>>代码示例>>Python>>正文


Python ElementTree.fromstring方法代码示例

本文整理汇总了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')
开发者ID:doubleO8,项目名称:versionone-sdk-spoon,代码行数:10,代码来源:connect_tests.py

示例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)
    }
开发者ID:jgmullor,项目名称:libsaas,代码行数:56,代码来源:xml.py

示例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
开发者ID:doubleO8,项目名称:versionone-sdk-spoon,代码行数:23,代码来源:client.py


注:本文中的lxml.etree.ElementTree.fromstring方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。