Python lxml.etree 模块,ElementDefaultClassLookup() 实例源码

我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用lxml.etree.ElementDefaultClassLookup()

项目:urban-journey    作者:urbanjourney    | 项目源码 | 文件源码
def from_string(ujml_string, file_name="<ujml_input>", globals=None) -> UjmlNode:
    """
    Used lo load in a ujml code from a string.


    :param string ujml_string: String containing the ujml code.
    :param string file_name: Source code file name.
    :param dict globals: Optional dictionary containing global values available in ujml local python interpreter
    :return: Ujml root node.
    :rtype: urban_journey.UjmlNode
    """

    parser = etree.XMLParser()
    lookup = etree.ElementDefaultClassLookup(element=UjmlElement)
    parser.set_element_class_lookup(lookup)
    root_elem = etree.fromstring(ujml_string, parser)
    ujml_node = UjmlNode(root_elem, file_name, globals or {})
    return ujml_node
项目:urban-journey    作者:urbanjourney    | 项目源码 | 文件源码
def from_file(file_path, globals=None):
    """
    Used lo load in a ujml code from a file.


    :param string file_path: Path to ujml file
    :param dict globals: Optional dictionary containing global values available in ujml local python interpreter
    :return: Ujml root node.
    :rtype: urban_journey.UjmlNode
    """

    file_path = os.path.abspath(file_path)
    with open(file_path) as f:
        source = f.read()
    parser = etree.XMLParser()
    lookup = etree.ElementDefaultClassLookup(element=UjmlElement)
    parser.set_element_class_lookup(lookup)
    root_elem = etree.fromstring(source, parser)
    ujml_node = UjmlNode(root_elem, file_path, globals or {})
    return ujml_node
项目:defusedxml    作者:tiran    | 项目源码 | 文件源码
def createDefaultParser(self):
        parser = _etree.XMLParser(**self.parser_config)
        element_class = self.element_class
        if self.element_class is not None:
            lookup = _etree.ElementDefaultClassLookup(element=element_class)
            parser.set_element_class_lookup(lookup)
        return parser
项目:pymods    作者:mrmiguez    | 项目源码 | 文件源码
def metadata(self):
        """
        Exposes the metadata content of an OAIRecord.
        :return: A reparsed root element either in the MODSRecord or DCRecord class, as appropriate.
        """
        record_data = self.find('./{*}metadata')
        if record_data is not None:
            try:
                if 'mods' in record_data[0].tag:
                    mods_parser_registration = etree.ElementDefaultClassLookup(element=MODSRecord)
                    mods_parser = etree.XMLParser()
                    mods_parser.set_element_class_lookup(mods_parser_registration)
                    return etree.XML(etree.tostring(record_data[0], encoding='UTF-8').decode('utf-8'),
                                     parser=mods_parser)
                elif 'qualified' in record_data[0].tag:
                    qdc_parser_registration = etree.ElementDefaultClassLookup(element=DCRecord)
                    qdc_parser = etree.XMLParser()
                    qdc_parser.set_element_class_lookup(qdc_parser_registration)
                    return etree.XML(etree.tostring(record_data[0], encoding='UTF-8').decode('utf-8'),
                                     parser=qdc_parser)
                elif 'dc' in record_data[0].tag:
                    dc_parser_registration = etree.ElementDefaultClassLookup(element=DCRecord)
                    dc_parser = etree.XMLParser()
                    dc_parser.set_element_class_lookup(dc_parser_registration)
                    return etree.XML(etree.tostring(record_data[0], encoding='UTF-8').decode('utf-8'),
                                     parser=dc_parser)
            except IndexError:
                pass
项目:pymods    作者:mrmiguez    | 项目源码 | 文件源码
def __init__(self, file_location):
        """
        Parser/iterator for the MODSRecord class. Iterates on mods:mods elements.
        :param file_location:
        """
        mods_parser_registration = etree.ElementDefaultClassLookup(element=MODSRecord)
        mods_parser = etree.XMLParser()
        mods_parser.set_element_class_lookup(mods_parser_registration)
        super(MODSReader, self).__init__(file_location, '{0}mods'.format(NAMESPACES['mods']), parser=mods_parser)
项目:pymods    作者:mrmiguez    | 项目源码 | 文件源码
def __init__(self, file_location):
        """
        Parser/iterator for the OAIRecord class. Iterates over record elements in any namespace (repox or oai-pmh).
        :param file_location:
        """
        oai_parser_registration = etree.ElementDefaultClassLookup(element=OAIRecord)
        oai_parser = etree.XMLParser()
        oai_parser.set_element_class_lookup(oai_parser_registration)
        super(OAIReader, self).__init__(file_location, '{*}record', parser=oai_parser)