LazyXML 1.2.1 Documentation

API Documentation

lazyxml – A simple xml parse and build lib

loads() – Load xml content to python object.

A simple xml parse and build lib.

lazyxml.loads(content, **kw)

Load xml content to python object.

>>> import lazyxml
>>> xml = '<demo><foo>foo</foo><bar>bar</bar></demo>'
>>> lazyxml.loads(xml)
{'bar': 'bar', 'foo': 'foo'}
>>> xml = '<demo><foo>foo</foo><bar>bar</bar></demo>'
>>> lazyxml.loads(xml, strip_root=False)
{'demo': {'bar': 'bar', 'foo': 'foo'}}
>>> xml = '<demo><foo>foo</foo><bar>1</bar><bar>2</bar></demo>'
>>> lazyxml.loads(xml)
{'bar': ['1', '2'], 'foo': 'foo'}
>>> xml = '<root xmlns:h="http://www.w3.org/TR/html4/">&lt;demo&gt;&lt;foo&gt;foo&lt;/foo&gt;&lt;bar&gt;bar&lt;/bar&gt;&lt;/demo&gt;</root>'
>>> lazyxml.loads(xml, unescape=True, strip_root=False)
{'root': {'demo': {'bar': 'bar', 'foo': 'foo'}}}
Parameters:content (str) – xml content

kw arguments below here.

Parameters:
  • encoding – XML编码 默认:utf-8.
  • unescape (bool) – 是否转换HTML实体 默认:False.
  • strip_root (bool) – 是否去除根节点 默认:True.
  • strip_attr (bool) – 是否去除节点属性 默认:True.
  • strip (bool) – 是否去除空白字符(换行符、制表符) 默认:True.
  • errors – 解码错误句柄 参考: str.decode() 默认:strict.
Return type:

dict

Changed in version 1.2.1: The strip_attr option supported to decide whether return the element attributes for parse result.

load() – Load xml content from file and convert to python object.

A simple xml parse and build lib.

lazyxml.load(fp, **kw)

Load xml content from file and convert to python object.

>>> import lazyxml
>>> with open('demo.xml', 'rb') as fp:
>>>     lazyxml.load(fp)
>>> from cStringIO import StringIO
>>> buffer = StringIO('<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[<foo>]]></foo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar></demo>')
>>> lazyxml.load(buffer)
{'bar': ['1', '2'], 'foo': '<foo>'}
>>> buffer.close()

Note

kw argument have the same meaning as in loads()

Parameters:fp – a file or file-like object that support .read() to read the xml content
Return type:dict

dumps() – Dump python object to xml.

A simple xml parse and build lib.

lazyxml.dumps(obj, **kw)

Dump python object to xml.

>>> import lazyxml
>>> data = {'demo':{'foo': '<foo>', 'bar': ['1', '2']}}
>>> lazyxml.dumps(data)
'<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[<foo>]]></foo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar></demo>'
>>> lazyxml.dumps(data, header_declare=False)
'<demo><foo><![CDATA[<foo>]]></foo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar></demo>'
>>> lazyxml.dumps(data, cdata=False)
'<?xml version="1.0" encoding="utf-8"?><demo><foo>&lt;foo&gt;</foo><bar>1</bar><bar>2</bar></demo>'
>>> print lazyxml.dumps(data, indent=' ' * 4)
<?xml version="1.0" encoding="utf-8"?>
<demo>
    <foo><![CDATA[<foo>]]></foo>
    <bar><![CDATA[1]]></bar>
    <bar><![CDATA[2]]></bar>
</demo>
>>> lazyxml.dumps(data, ksort=True)
'<?xml version="1.0" encoding="utf-8"?><demo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar><foo><![CDATA[<foo>]]></foo></demo>'
>>> lazyxml.dumps(data, ksort=True, reverse=True)
'<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[<foo>]]></foo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar></demo>'

Note

Data that has attributes convert to xml see demo/dump.py.

Parameters:obj – data for dump to xml.

kw arguments below here.

Parameters:
  • encoding – XML编码 默认:utf-8.
  • header_declare (bool) – 是否声明XML头部 默认:True.
  • root – XML根节点 默认:None.
  • cdata (bool) – 是否使用XML CDATA格式 默认:True.
  • indent – XML层次缩进 默认:None.
  • ksort (bool) – XML标签是否排序 默认:False.
  • reverse (bool) – XML标签排序时是否倒序 默认:False.
  • errors – 解码错误句柄 see: str.decode() 默认:strict.
  • hasattr (bool) – 是否包含属性 默认:False.
  • attrkey – 标签属性标识key 默认:{attrs}.
  • valuekey – 标签值标识key 默认:{values}.
Return type:

str

dump() – Dump python object to file.

A simple xml parse and build lib.

lazyxml.dump(obj, fp, **kw)

Dump python object to file.

>>> import lazyxml
>>> data = {'demo': {'foo': 1, 'bar': 2}}
>>> lazyxml.dump(data, 'dump.xml')
>>> with open('dump-fp.xml', 'w') as fp:
>>>     lazyxml.dump(data, fp)
>>> from cStringIO import StringIO
>>> data = {'demo': {'foo': 1, 'bar': 2}}
>>> buffer = StringIO()
>>> lazyxml.dump(data, buffer)
>>> buffer.getvalue()
<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[1]]></foo><bar><![CDATA[2]]></bar></demo>
>>> buffer.close()

Note

kw argument have the same meaning as in dumps()

Parameters:
  • obj – data for dump to xml.
  • fp – a filename or a file or file-like object that support .write() to write the xml content

Changed in version 1.2: The fp is a filename of string before this. It can now be a file or file-like object that support .write() to write the xml content.

builder – XML Builder Module

class lazyxml.builder.Builder(**kw)

XML Builder

set_options(**kw)

Set Builder options.

See also

kw argument have the same meaning as in lazyxml.dumps()

get_options()

Get Builder options.

dict2xml(data)

Convert dict to xml.

Warning

DEPRECATED: dict2xml() is deprecated. Please use object2xml() instead.

Deprecated since version 1.2.

object2xml(data)

Convert python object to xml string.

Parameters:data – data for build xml. If don’t provide the root option, type of data must be dict and len(data) == 1.
Return type:str or unicode

New in version 1.2.

build_xml_header()

Build xml header include version and encoding.

Return type:str
build_tree(data, tagname, attrs=None, depth=0)

Build xml tree.

Parameters:
  • data – data for build xml.
  • tagname – element tag name.
  • attrs (dict or None) – element attributes. Default:None.
  • depth (int) – element depth of the hierarchy. Default:0.
check_structure(keys)

Check structure availability by attrkey and valuekey option.

pickdata(data)

Pick data from attrkey and valuekey option.

Returns:a pair of (attrs, values)
Return type:tuple
safedata(data, cdata=True)

Convert xml special chars to entities.

Parameters:
  • data – the data will be converted safe.
  • cdata (bool) – whether to use cdata. Default:True. If not, use cgi.escape() to convert data.
Return type:

str

build_tag(tag, text='', attrs=None)

Build tag full info include the attributes.

Parameters:
  • tag – tag name.
  • text – tag text.
  • attrs (dict or None) – tag attributes. Default:None.
Return type:

str

build_attr(attrs)

Build tag attributes.

Parameters:attrs (dict) – tag attributes
Return type:str
tag_start(tag, attrs=None)

Build started tag info.

Parameters:
  • tag – tag name
  • attrs (dict or None) – tag attributes. Default:None.
Return type:

str

tag_end(tag)

Build closed tag info.

Parameters:tag – tag name
Return type:str

parser – XML Parser Module

class lazyxml.parser.Parser(**kw)

XML Parser

set_options(**kw)

Set Parser options.

See also

kw argument have the same meaning as in lazyxml.loads()

get_options()

Get Parser options.

xml2dict(content)

Convert xml content to dict.

Warning

DEPRECATED: xml2dict() is deprecated. Please use xml2object() instead.

Deprecated since version 1.2.

xml2object(content)

Convert xml content to python object.

Parameters:content – xml content
Return type:dict

New in version 1.2.

xml_filter(content)

Filter and preprocess xml content

Parameters:content – xml content
Return type:str
guess_xml_encoding(content)

Guess encoding from xml header declaration.

Parameters:content – xml content
Return type:str or None
strip_xml_header(content)

Strip xml header

Parameters:content – xml content
Return type:str
parse(element)

Parse xml element.

Parameters:element – an Element instance
Return type:dict
parse_full(element)

Parse xml element include the node attributes.

Parameters:element – an Element instance
Return type:dict

New in version 1.2.1.

get_node(element)

Get node info.

Parse element and get the element tag info. Include tag name, value, attribute, namespace.

Parameters:element – an Element instance
Return type:dict
split_namespace(tag)

Split tag namespace.

Parameters:tag – tag name
Returns:a pair of (namespace, tag)
Return type:tuple

Changes

See the Changelog for a full list of changes to lazyxml.

About This Documentation

This documentation is generated using the Sphinx documentation generator. The source files for the documentation are located in the docs/ directory of the lazyxml distribution. To generate the docs locally run the following command from the docs/ directory of the lazyxml source:

$ cd docs
$ make html

or use make help to generate other format.

Indices and tables