Python 之lxml库学习笔记三

序列化:

序列化通常使用tostring()方法来返回一个字符串,或者ElementTree.write()方法来写入一个文件,一个类文件的对象,或者一个URL(通过FTP的PUT或者HTTP的POST)。二者都使用相同的关键字参数比如pretty_print来格式化输出或者encoding来选择一个特定的输出编码而不是简单的ASCII。
>>> root = etree.XML("<root><a><b/></a></root>")
>>> etree.tostring(root)
’<root><a><b/></a></root>’

>>> print etree.tostring(root, xml_declaration=True)
<?xml version=’1.0’ encoding=’ASCII’?>
<root><a><b/></a></root>

>>> print etree.tostring(root, encoding="iso-8859-1")
<?xml version=’1.0’ encoding=’iso-8859-1’?>
<root><a><b/></a></root>

>>> print etree.tostring(root, pretty_print=True)
<root>
<a>
<b/>
</a>
</root>

Note that pretty printing appends a newline at the end.

注意pretty打印在末尾添加一个新行。

从lxml2.0起,serialisation可以做的不止XML序列化,可以序列化到HTML或者通过传递函数关键字来提取文本内容。

>>> root = etree.XML("<html><head/><body><p>Hello<br/>World</p></body></html>")
>>> etree.tostring(root) # default: method = ’xml’
’<html><head/><body><p>Hello<br/>World</p></body></html>’
>>> etree.tostring(root, method="xml") # same as above
’<html><head/><body><p>Hello<br/>World</p></body></html>’
>>> etree.tostring(root, method="html")
’<html><head></head><body><p>Hello<br>World</p></body></html>’

>>> print etree.tostring(root, method="html", pretty_print=True)
<html>
<head></head>
<body><p>Hello<br>World</p></body>
</html>

>>> etree.tostring(root, method="text")
b’HelloWorld’

对XML序列化而言,默认的文本编码是ASCII

>>> br = root.find(".//br")
>>> br.tail = u"W\xf6rld"
>>> etree.tostring(root, method="text") # doctest: +ELLIPSIS
Traceback (most recent call last):
...
UnicodeEncodeError: ’ascii’ codec can’t encode character u’\xf6’ ...
>>>etree.tostring(root, method="text", encoding="UTF-8")
b’HelloW\xc3\xb6rld’

>>> etree.tostring(root, encoding=unicode, method="text")
u’HelloW\xf6rld’

ElementTree类:

一个ElementTree主要是围绕在一个有根节点的树的文档包装类。它提供了很多方法来解析,序列化以及一般的文档处理。一个最大的区别是它作为一个整体文档来序列化。与之相对的是序列化成单个的元素。

>>> tree = etree.parse(StringIO("""\
 <?xml version="1.0"?>
 <!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "eggs"> ]>
 <root>
 <a>&tasty;</a>
 </root>
 """))
>>> print(tree.docinfo.doctype)
<!DOCTYPE root SYSTEM "test">

>>> # lxml 1.3.4 and later
>>> print(etree.tostring(tree))
<!DOCTYPE root SYSTEM "test" [
<!ENTITY tasty "eggs">
]>
<root>
<a>eggs</a>
</root>

>>> # lxml 1.3.4 and later
>>> print(etree.tostring(etree.ElementTree(tree.getroot())))
<!DOCTYPE root SYSTEM "test" [
<!ENTITY tasty "eggs">
]>
<root>
<a>eggs</a>
</root>

>>> # ElementTree and lxml <= 1.3.3
>>> print(etree.tostring(tree.getroot()))
<root>
<a>eggs</a>
</root>

从字符串和文件中解析:

fromstring()是解析字符串最容易的方法

>>> some_xml_data = "<root>data</root>"
>>> root = etree.fromstring(some_xml_data)
>>> print root.tag
root
>>> etree.tostring(root)
’<root>data</root>’

XML()方法和fromstring()方法类似,但它主要用来把XML文字写入源文件。
>>> root = etree.XML("<root>data</root>")
>>> print root.tag
root
>>> etree.tostring(root)
’<root>data</root>’

parse()方法用来从文件或者类文件对象中解析
>>> some_file_like = StringIO.StringIO("<root>data</root>")
>>> tree = etree.parse(some_file_like)
>>> etree.tostring(tree)
’<root>data</root>’

注意parse()返回的是一个ElementTree对象,而不是字符串解析方法的Element对象。

>>> root = tree.getroot()
>>> print root.tag
root
>>> etree.tostring(root)
’<root>data</root>’

posted @ 2010-06-02 14:43  小楼  阅读(6262)  评论(0编辑  收藏  举报