简体   繁体   English

python lxml.etree._Element 到 JSON 或 dict

[英]python lxml.etree._Element to JSON or dict

From one method of library I get lxml.etree._Element , is there any library or function to convert an lxml.etree._Element to JSON or a dictionary?从我得到的一种库方法lxml.etree._Element ,是否有任何库或函数可以将lxml.etree._Element转换为 JSON 或字典?

For example:例如:

<detail>
    <ouaf:Fault xmlns:ouaf="urn:oracle:ouaf">
        <ResponseStatus>F</ResponseStatus>
        <ResponseCode>2013</ResponseCode>
        <ResponseData numParm="1"  text="The personal account was not found: 9134211141"  category="90006"  number="32200"  parm1="9134211141"  />
    </ouaf:Fault>
</detail>

Should be something like this:应该是这样的:

{
    'detail': {
        'Fault': {
            'ResponseStatus': 'F'
            'ResponseCode': '2013'
            'ResponseData': {
                'numParm': '1'
                'text': 'The personal account was not found: 9134211141'
                'category': '90006'
                'number': '32200'
                'parm1': '9134211141'
            }           
        }
    }
}

Update 1:更新 1:

When I trying use this function当我尝试使用此功能时

def conver_element(self, element):
        foo = self.recursive_dict(element)
        return foo

def recursive_dict(self, element):
    return element.tag, \
           dict(map(self.recursive_dict, element)) or element.text

And I get foo:我得到了 foo:

<class 'tuple'>: ('detail', {'ResponseCode': '2013', 'ResponseStatus': 'F', 'ResponseData': None})

The recursive_dict method in the linked documentation doesn't include XML attribute in the result.链接文档中的recursive_dict方法在结果中不包含 XML 属性。 Assuming, for XML element that has attribute and doesn't have content (self-closing tag) you want to get the attributes as the dictionary value, the following modified version of recursive_dict will do :假设,对于具有属性但不具有内容(自关闭标记)的 XML 元素,您希望将属性作为字典值获取,以下修改后的recursive_dict版本将执行以下操作:

def recursive_dict(element):
    if element.text == None and len(element.attrib):
        return element.tag, element.attrib
    return element.tag, \
            dict(map(recursive_dict, element)) or element.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LXML/Python - 遍历 lxml.etree._Element 列表 - LXML/Python - Looping over a list of lxml.etree._Element 解析lxml.etree._Element内容 - Parsing lxml.etree._Element contents 从lxml.etree._Element获取价值 - Get value from lxml.etree._Element 装饰lxml.etree._Element方法 - decorate lxml.etree._Element methods python lxml 3.3.5 - 加载代码时出错 - “ValueError:lxml.etree._Element的大小错误,请尝试重新编译” - python lxml 3.3.5 - error on loading code - “ValueError: lxml.etree._Element has the wrong size, try recompiling” 如何从zeep python库解析soap Fault.detail(lxml.etree._Element) - How parse soap Fault.detail(lxml.etree._Element) from zeep python library 令人难以置信的基本lxml问题:获取lxml.etree._Element的HTML /字符串内容? - Incredibly basic lxml questions: getting HTML/string content of lxml.etree._Element? lxml: cssselect(): AttributeError: &#39;lxml.etree._Element&#39; 对象没有属性 &#39;cssselect&#39; - lxml: cssselect(): AttributeError: 'lxml.etree._Element' object has no attribute 'cssselect' 真的很奇怪……无法设置内置/扩展类型“lxml.etree._Element”的属性 - Really weird… can't set attributes of built-in/extension type 'lxml.etree._Element' python&lxml:将空元素添加到etree - python & lxml: adding empty element into an etree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM