當前位置: 首頁>>代碼示例>>Python>>正文


Python etree._ElementTree方法代碼示例

本文整理匯總了Python中lxml.etree._ElementTree方法的典型用法代碼示例。如果您正苦於以下問題:Python etree._ElementTree方法的具體用法?Python etree._ElementTree怎麽用?Python etree._ElementTree使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在lxml.etree的用法示例。


在下文中一共展示了etree._ElementTree方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: format

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def format(self, diff, orig_tree, differ=None):
        # Make a new tree, both because we want to add the diff namespace
        # and also because we don't want to modify the original tree.
        result = deepcopy(orig_tree)
        if isinstance(result, etree._ElementTree):
            root = result.getroot()
        else:
            root = result

        etree.register_namespace(DIFF_PREFIX, DIFF_NS)

        for action in diff:
            self.handle_action(action, root)

        self.finalize(root)

        etree.cleanup_namespaces(result, top_nsmap={DIFF_PREFIX: DIFF_NS})
        return self.render(result) 
開發者ID:Shoobx,項目名稱:xmldiff,代碼行數:20,代碼來源:formatting.py

示例2: set_trees

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def set_trees(self, left, right):
        self.clear()

        # Make sure we were passed two lxml elements:
        if isinstance(left, etree._ElementTree):
            left = left.getroot()
        if isinstance(right, etree._ElementTree):
            right = right.getroot()

        if not (etree.iselement(left) and etree.iselement(right)):
            raise TypeError("The 'left' and 'right' parameters must be "
                            "lxml Elements.")

        # Left gets modified as a part of the diff, deepcopy it first.
        self.left = deepcopy(left)
        self.right = right 
開發者ID:Shoobx,項目名稱:xmldiff,代碼行數:18,代碼來源:diff.py

示例3: load_track_from_xml

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def load_track_from_xml(self, path):
        xml = etree.parse(path)
        root = xml.getroot()  # type: etree._ElementTree
        for child_of_root in root:
            if child_of_root.tag == "s":
                self.s = self.get_1d_array_from_xml(child_of_root)
            elif child_of_root.tag == "phi":
                self.phi = self.get_1d_array_from_xml(child_of_root)
            elif child_of_root.tag == "control_points_s":
                self.control_points_s = self.get_1d_array_from_xml(child_of_root)
            elif child_of_root.tag == "left":
                self.cones_left = self.get_array_from_xml(child_of_root)
            elif child_of_root.tag == "right":
                self.cones_right = self.get_array_from_xml(child_of_root)
            elif child_of_root.tag == "middle":
                self.middle = self.get_array_from_xml(child_of_root)
            elif child_of_root.tag == "control_points":
                self.control_points = self.get_array_from_xml(child_of_root)
        self.computed_cones = True 
開發者ID:AMZ-Driverless,項目名稱:fssim,代碼行數:21,代碼來源:track.py

示例4: match_xpath

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def match_xpath(xpath, doc):
    """Return a match of expression `xpath` against document `doc`.

    :type xpath: Either `unicode` or `etree.XPath`
    :type doc: Either `etree._ElementTree` or `etree.XPathDocumentEvaluator`

    :rtype: bool
    """
    is_xpath_compiled = is_compiled_xpath(xpath)
    is_doc_compiled = is_compiled_doc(doc)

    if is_xpath_compiled and is_doc_compiled:
        return doc(xpath.path)
    elif is_xpath_compiled:
        return xpath(doc)
    elif is_doc_compiled:
        return doc(xpath)
    else:
        return doc.xpath(xpath) 
開發者ID:maas,項目名稱:maas,代碼行數:21,代碼來源:xpath.py

示例5: try_match_xpath

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def try_match_xpath(xpath, doc, logger=logging):
    """See if the XPath expression matches the given XML document.

    Invalid XPath expressions are logged, and are returned as a
    non-match.

    :type xpath: Either `unicode` or `etree.XPath`
    :type doc: Either `etree._ElementTree` or `etree.XPathDocumentEvaluator`

    :rtype: bool
    """
    try:
        # Evaluating an XPath expression against a document with LXML
        # can return a list or a string, and perhaps other types.
        # Casting the return value into a boolean context appears to
        # be the most reliable way of detecting a match.
        return bool(match_xpath(xpath, doc))
    except etree.XPathEvalError as error:
        # Get a plaintext version of `xpath`.
        expr = xpath.path if is_compiled_xpath(xpath) else xpath
        logger.warning("Invalid expression '%s': %s", expr, str(error))
        return False 
開發者ID:maas,項目名稱:maas,代碼行數:24,代碼來源:xpath.py

示例6: __init__

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def __init__(self, input):
        if isinstance(input, etree._Element):
            self.xml_doc = input
        elif isinstance(input, etree._ElementTree):
            self.xml_doc = input.getroot()
        elif KBinXML.is_binary_xml(input):
            self.from_binary(input)
        else:
            self.from_text(input) 
開發者ID:mon,項目名稱:kbinxml,代碼行數:11,代碼來源:kbinxml.py

示例7: test_perform_query

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def test_perform_query(self):
        api = RomeoAPI(api_key=None)
        with requests_mock.mock() as http_mocker:
            http_mocker.get('http://www.sherpa.ac.uk/romeo/api29.php?issn=0022-328X',
                content=self.issn_response)
            http_mocker.get('http://www.sherpa.ac.uk/romeo/api29.php?jtitle=Physical%20Review%20E',
                content=self.jtitle_response)

            self.assertIsInstance(api.perform_romeo_query(
                {'issn': '0022-328X'}), etree._ElementTree)
            self.assertIsInstance(api.perform_romeo_query(
                {'jtitle': 'Physical Review E'}), etree._ElementTree) 
開發者ID:dissemin,項目名稱:dissemin,代碼行數:14,代碼來源:test_romeo.py

示例8: _get_etree_for_text

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def _get_etree_for_text(text: str) -> _ElementTree:
    return etree.ElementTree(fromstring(text)) 
開發者ID:HazyResearch,項目名稱:fonduer,代碼行數:4,代碼來源:structural.py

示例9: test_xml

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def test_xml(self, http):
        request = Request("GET", "https://httpbin.org/xml")
        context_http_response = ContextHttpResponse(http, request)
        assert isinstance(context_http_response.xml, etree._ElementTree) 
開發者ID:alephdata,項目名稱:memorious,代碼行數:6,代碼來源:test_http.py

示例10: load_html

# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import _ElementTree [as 別名]
def load_html(htmlobject):
    """Load object given as input and validate its type
    (accepted: LXML tree, bytestring and string)
    """
    # use tree directly
    if isinstance(htmlobject, (etree._ElementTree, html.HtmlElement)):
        return htmlobject
    tree = None
    check_flag = False
    # try to detect encoding and convert to string
    if isinstance(htmlobject, bytes):
        # test
        if 'html' not in htmlobject[:50].decode(encoding='ascii', errors='ignore'):
            check_flag = True
        guessed_encoding = detect_encoding(htmlobject)
        if guessed_encoding is not None:
            if guessed_encoding == 'UTF-8':
                tree = html.fromstring(htmlobject, parser=HTML_PARSER)
            else:
                try:
                    htmlobject = htmlobject.decode(guessed_encoding)
                    tree = html.fromstring(htmlobject, parser=HTML_PARSER)
                except UnicodeDecodeError:
                    LOGGER.warning('encoding issue: %s', guessed_encoding)
                    tree = html.fromstring(htmlobject, parser=RECOVERY_PARSER)
        else:
            tree = html.fromstring(htmlobject, parser=RECOVERY_PARSER)
    # use string if applicable
    elif isinstance(htmlobject, str):
        # test
        if 'html' not in htmlobject[:50]:
            check_flag = True
        try:
            tree = html.fromstring(htmlobject, parser=HTML_PARSER)
        except ValueError:
            # try to parse a bytestring
            try:
                tree = html.fromstring(htmlobject.encode('utf8'), parser=HTML_PARSER)
            except Exception as err:
                LOGGER.error('parser bytestring %s', err)
        except Exception as err:
            LOGGER.error('parsing failed: %s', err)
    # default to None
    else:
        LOGGER.error('this type cannot be processed: %s', type(htmlobject))
    # further test
    # test if it's HTML
    if tree is not None and check_flag is True:
        if len(tree) < 2:
            LOGGER.error('Parse tree empty: not valid HTML')
            tree = None
    #if tree is None:
    #    if isinstance(htmlobject, bytes) or isinstance(htmlobject, str):
    #        # more robust parsing
    #        tree = fromsoup(htmlobject)
    return tree 
開發者ID:adbar,項目名稱:trafilatura,代碼行數:58,代碼來源:utils.py


注:本文中的lxml.etree._ElementTree方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。