easyxml是xml文档的简单对象表示。
easy-xml的Python项目详细描述
easyxml提供了xml文档的简化视图。
XML文档中的元素,包括根级文档, 由easyxml对象表示。每个easyxml对象都有一个 \u name属性,包含由 easyxml对象,一个名为\u attrs的字典,保存元素的 属性,以及一个名为\u content的包含内容的序列 元素的。内容序列包含 元素中的字符数据(从 开始和结束),以及嵌套XML的其他EasyXML对象 元素。
easyxml对象的内容可以直接访问,就像它 是一个序列本身。此外,元素的属性 它们可以通过easyxml对象上的名称访问。
例如,xml片段<zip code="12345">Schenectady,NY</zip>将作为easyxml对象返回,其中name``u name`` 是'zip',name```u attrs``是{'code': '12345'},name。 _content是['Schenectady, NY'],name``code``是'12345', 而name``[0]``是'Schenectady, NY'。
用法:
# Import the EasyXML class from easy_xml import EasyXML # Parse the file-like object `stream` into an EasyXML structure. xmldoc = EasyXML.parseXML(stream) # Find the first ``<hello>`` node in the document. # If it exists, record its content in the `hello` variable. # If not, record ``world`` in `hello`, and create a new # ``<hello>world</hello>`` XML element at the top of the document. for element in xmldoc: if isinstance(element,EasyXML) and element._name == 'hello': hello = ' '.join(element._content) break if hello is None: hello = 'world' element = EasyXML('hello', content=[hello]) xmldoc._content.insert(0, element) # Convert the EasyXML structure back into a valid XML document. xmlfile = str(xmldoc)
注意:
- 忽略处理说明和注释。
- 仅基本实体处理(<、>和&) 执行。