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/"><demo><foo>foo</foo><bar>bar</bar></demo></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: Return type: 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 inloads()
Parameters: fp – a file or file-like object that support .read()
to read the xml contentReturn 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><foo></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: - encoding – XML编码 默认:
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 indumps()
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 inlazyxml.dumps()
-
get_options
()¶ Get Builder options.
-
dict2xml
(data)¶ Convert dict to xml.
Warning
DEPRECATED:
dict2xml()
is deprecated. Please useobject2xml()
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 ofdata
must be dict andlen(data) == 1
.Return type: str or unicode New in version 1.2.
-
build_tree
(data, tagname, attrs=None, depth=0)¶ Build xml tree.
Parameters:
-
check_structure
(keys)¶ Check structure availability by
attrkey
andvaluekey
option.
-
pickdata
(data)¶ Pick data from
attrkey
andvaluekey
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, usecgi.escape()
to convert data.
Return type:
-
build_tag
(tag, text='', attrs=None)¶ Build tag full info include the attributes.
Parameters: Return type:
-
tag_start
(tag, attrs=None)¶ Build started tag info.
Parameters: Return type:
-
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 inlazyxml.loads()
-
get_options
()¶ Get Parser options.
-
xml2dict
(content)¶ Convert xml content to dict.
Warning
DEPRECATED:
xml2dict()
is deprecated. Please usexml2object()
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
-
parse_full
(element)¶ Parse xml element include the node attributes.
Parameters: element – an Element
instanceReturn type: dict New in version 1.2.1.
-
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.