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


Python libxml2.parseDoc方法代碼示例

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


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

示例1: parse

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def parse(self, xml, pvars):
        import libxml2
        doc = libxml2.parseDoc(xml)
        
        pvars = self.getVars(pvars)
        vals = {}
        for k, v in pvars.items():
            res = doc.xpathEval(v)
            vals[k] = []
            for r in res:
                
                #if not vals.has_key(r.name):
                #   vals[r.name] = []
                
                if r.type == 'element':
                    #vals[r.name].append(self.xmlToDict(minidom.parseString(str(r)))[r.name])
                    vals[k].append(self.xmlToDict(minidom.parseString(str(r)))[r.name])
                elif r.type == 'attribute':
                    vals[k].append(r.content)
                else:
                    logger.error("UNKNOWN TYPE")
            
            if len(vals[k]) == 1:
                vals[k] = vals[k][0]
            elif len(vals[k]) == 0:
                vals[k] = None

        return vals 
開發者ID:svub,項目名稱:whatsapp-rest-webservice,代碼行數:30,代碼來源:waresponseparser.py

示例2: get_xml_path

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def get_xml_path(xml, path=None, func=None):
    """
    Return the content from the passed xml xpath, or return the result
    of a passed function (receives xpathContext as its only arg)
    """
    doc = None
    ctx = None
    result = None

    try:
        doc = libxml2.parseDoc(xml)
        ctx = doc.xpathNewContext()

        if path:
            ret = ctx.xpathEval(path)
            if ret is not None:
                if type(ret) == list:
                    if len(ret) >= 1:
                        result = ret[0].content
                else:
                    result = ret

        elif func:
            result = func(ctx)

        else:
            raise ValueError("'path' or 'func' is required.")
    finally:
        if doc:
            doc.freeDoc()
        if ctx:
            ctx.xpathFreeContext()
    return result 
開發者ID:welliamcao,項目名稱:VManagePlatform,代碼行數:35,代碼來源:vMUtil.py

示例3: systemid

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def systemid(self):
        systemid = None
        xpath_str = "//member[name='system_id']/value/string"

        if os.path.isfile(self.config['systemIdPath']):
            fd = open(self.config['systemIdPath'], 'r')
            xml_data = fd.read()
            fd.close()

            # Ugh, xml parsing time ...
            # First, try parsing with libxml2 ...
            if systemid is None:
                try:
                    import libxml2
                    doc = libxml2.parseDoc(xml_data)
                    ctxt = doc.xpathNewContext()
                    systemid = ctxt.xpathEval(xpath_str)[0].content
                    doc.freeDoc()
                    ctxt.xpathFreeContext()
                except ImportError:
                    pass

            # m-kay, let's try with lxml now ...
            if systemid is None:
                try:
                    from lxml import etree
                    root = etree.fromstring(xml_data)
                    systemid = root.xpath(xpath_str)[0].text
                except ImportError:
                    pass

            # Strip the 'ID-' prefix
            if systemid is not None and systemid.startswith('ID-'):
                systemid = systemid[3:]

        return int(systemid) 
開發者ID:YoLoveLife,項目名稱:DevOps,代碼行數:38,代碼來源:rhn_register.py

示例4: _xmlParseDoc

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def _xmlParseDoc(self, sXml):
        """
        Parse a serialized XML and return a DOM, which the caller must free later on!
        """
        return libxml2.parseDoc(sXml.encode(utf8)) 
開發者ID:Transkribus,項目名稱:TranskribusPyClient,代碼行數:7,代碼來源:client.py

示例5: handle_valid

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def handle_valid(node, schema):
    global log
    global nb_instances_success
    global nb_instances_failed

    instance = ""
    child = node.children
    while child != None:
        if child.type != 'text':
	    instance = instance + child.serialize()
	child = child.next

    try:
	doc = libxml2.parseDoc(instance)
    except:
        doc = None

    if doc == None:
        log.write("\nFailed to parse correct instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
	return

    try:
        ctxt = schema.relaxNGNewValidCtxt()
	ret = doc.relaxNGValidateDoc(ctxt)
    except:
        ret = -1
    if ret != 0:
        log.write("\nFailed to validate correct instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
    else:
	nb_instances_success = nb_instances_success + 1
    doc.freeDoc()

#
# handle an invalid instance
# 
開發者ID:flutter,項目名稱:buildroot,代碼行數:43,代碼來源:check-relaxng-test-suite.py

示例6: handle_invalid

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def handle_invalid(node, schema):
    global log
    global nb_instances_success
    global nb_instances_failed

    instance = ""
    child = node.children
    while child != None:
        if child.type != 'text':
	    instance = instance + child.serialize()
	child = child.next

    try:
	doc = libxml2.parseDoc(instance)
    except:
        doc = None

    if doc == None:
        log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	return

    try:
        ctxt = schema.relaxNGNewValidCtxt()
	ret = doc.relaxNGValidateDoc(ctxt)
    except:
        ret = -1
    if ret == 0:
        log.write("\nFailed to detect validation problem in instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
    else:
	nb_instances_success = nb_instances_success + 1
    doc.freeDoc()

#
# handle an incorrect test
# 
開發者ID:flutter,項目名稱:buildroot,代碼行數:42,代碼來源:check-relaxng-test-suite.py

示例7: get_xml_path

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def get_xml_path(xml, path=None, func=None):
        """
        Return the content from the passed xml xpath, or return the result
        of a passed function (receives xpathContext as its only arg)
        """
        doc = None
        ctx = None
        result = None

        try:
            doc = libxml2.parseDoc(xml)
            ctx = doc.xpathNewContext()

            if path:
                ret = ctx.xpathEval(path)
                if ret is not None:
                    if type(ret) == list:
                        if len(ret) >= 1:
                            result = ret[0].content
                    else:
                        result = ret

            elif func:
                result = func(ctx)

            else:
                raise ValueError("'path' or 'func' is required.")
        finally:
            if doc:
                doc.freeDoc()
            if ctx:
                ctx.xpathFreeContext()
        return result 
開發者ID:xiaomatech,項目名稱:ops,代碼行數:35,代碼來源:kvm.py

示例8: handle_valid

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def handle_valid(node, schema):
    global log
    global nb_instances_success
    global nb_instances_failed

    instance = node.prop("dtd")
    if instance == None:
        instance = ""
    child = node.children
    while child != None:
        if child.type != 'text':
	    instance = instance + child.serialize()
	child = child.next

    mem = libxml2.debugMemory(1);
    try:
	doc = libxml2.parseDoc(instance)
    except:
        doc = None

    if doc == None:
        log.write("\nFailed to parse correct instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
	return

    if debug:
        print "instance line %d" % (node.lineNo())
       
    try:
        ctxt = schema.relaxNGNewValidCtxt()
	ret = doc.relaxNGValidateDoc(ctxt)
	del ctxt
    except:
        ret = -1

    doc.freeDoc()
    if mem != libxml2.debugMemory(1):
	print "validating instance %d line %d leaks" % (
		  nb_instances_tests, node.lineNo())

    if ret != 0:
        log.write("\nFailed to validate correct instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
    else:
	nb_instances_success = nb_instances_success + 1

#
# handle an invalid instance
# 
開發者ID:flutter,項目名稱:buildroot,代碼行數:55,代碼來源:check-xsddata-test-suite.py

示例9: handle_invalid

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def handle_invalid(node, schema):
    global log
    global nb_instances_success
    global nb_instances_failed

    instance = node.prop("dtd")
    if instance == None:
        instance = ""
    child = node.children
    while child != None:
        if child.type != 'text':
	    instance = instance + child.serialize()
	child = child.next

#    mem = libxml2.debugMemory(1);

    try:
	doc = libxml2.parseDoc(instance)
    except:
        doc = None

    if doc == None:
        log.write("\nStrange: failed to parse incorrect instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	return

    if debug:
        print "instance line %d" % (node.lineNo())
       
    try:
        ctxt = schema.relaxNGNewValidCtxt()
	ret = doc.relaxNGValidateDoc(ctxt)
	del ctxt

    except:
        ret = -1

    doc.freeDoc()
#    if mem != libxml2.debugMemory(1):
#	print "validating instance %d line %d leaks" % (
#		  nb_instances_tests, node.lineNo())
    
    if ret == 0:
        log.write("\nFailed to detect validation problem in instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
    else:
	nb_instances_success = nb_instances_success + 1

#
# handle an incorrect test
# 
開發者ID:flutter,項目名稱:buildroot,代碼行數:56,代碼來源:check-xsddata-test-suite.py

示例10: handle_valid

# 需要導入模塊: import libxml2 [as 別名]
# 或者: from libxml2 import parseDoc [as 別名]
def handle_valid(node, schema):
    global log
    global nb_instances_success
    global nb_instances_failed

    instance = node.prop("dtd")
    if instance == None:
        instance = ""
    child = node.children
    while child != None:
        if child.type != 'text':
	    instance = instance + child.serialize()
	child = child.next

#    mem = libxml2.debugMemory(1);
    try:
	doc = libxml2.parseDoc(instance)
    except:
        doc = None

    if doc == None:
        log.write("\nFailed to parse correct instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
	return

    if debug:
        print "instance line %d" % (node.lineNo())
       
    try:
        ctxt = schema.relaxNGNewValidCtxt()
	ret = doc.relaxNGValidateDoc(ctxt)
	del ctxt
    except:
        ret = -1

    doc.freeDoc()
#    if mem != libxml2.debugMemory(1):
#	print "validating instance %d line %d leaks" % (
#		  nb_instances_tests, node.lineNo())

    if ret != 0:
        log.write("\nFailed to validate correct instance:\n-----\n")
	log.write(instance)
        log.write("\n-----\n")
	nb_instances_failed = nb_instances_failed + 1
    else:
	nb_instances_success = nb_instances_success + 1

#
# handle an invalid instance
# 
開發者ID:flutter,項目名稱:buildroot,代碼行數:55,代碼來源:check-relaxng-test-suite2.py


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