Author: jmorliaguet Date: Sat Apr 29 18:45:02 2006 New Revision: 2985 Added: cpsskins/branches/paris-sprint-2006/setup/DOM.txt (contents, props changed) cpsskins/branches/paris-sprint-2006/tests/test_dom.py (contents, props changed) Log:
- added a doc test for the xml.dom API Added: cpsskins/branches/paris-sprint-2006/setup/DOM.txt ============================================================================== --- (empty file) +++ cpsskins/branches/paris-sprint-2006/setup/DOM.txt Sat Apr 29 18:45:02 2006 @@ -0,0 +1,82 @@ +Integration with xml.dom (the Document Object Model API) +-------------------------------------------------------- + +The resource importer and exporter use the DOM API (1). + + >>> from xml.dom.minidom import DOMImplementation + +we need a document: + + >>> dom = DOMImplementation() + >>> doc = dom.createDocument(None, u'themes', None) + >>> root = doc.documentElement + + >>> print doc.toxml() + <?xml version="1.0" ?> + <themes/> + +elements are created with createElement(tagname) and added to parent node with +appendChild(element), setAttribute(name, value) is used to set attributes: + + >>> element = doc.createElement(u'theme') + >>> element.setAttribute(u'title', u'A theme') + >>> element.setAttribute(u'uri', u'cpsskins://canvas-theme:12345') + + >>> theme = root.appendChild(element) + +doc.toxml() creates an XML representation of the document: + + >>> print doc.toxml() + <?xml version="1.0" ?> + <themes><theme title="A theme" uri="cpsskins://canvas-theme:12345"/></themes> + + +Exporting nested tree structures +-------------------------------- + +Hierarchical DOM structures can be created using a recursive traversal. + + >>> from cpsskins import elements + >>> tree = elements.theme.Theme(u'A Theme') + >>> tree[u'page'] = elements.themepage.ThemePage(u'A Theme page') + >>> tree[u'page'][u'top'] = elements.pageblock.PageBlock(u'Top block') + >>> tree[u'page'][u'top'][u'left'] = elements.cell.Cell(u'Left cell') + >>> tree[u'page'][u'top'][u'main'] = elements.cell.Cell(u'Main cell') + +Let us traverse the entire tree recursively: + + >>> def traverse(node): + ... print node + ... for child in node: + ... traverse(node[child]) + + >>> traverse(tree) + Theme('A Theme') + ThemePage('A Theme page') + PageBlock('Top block') + Cell('Left cell') + Cell('Main cell') + +Now we traverse the tree again, but we convert each node into a DOM node by +preserving the orginal tree structure: + + >>> doc = dom.createDocument(None, u'themes', None) + >>> root = doc.documentElement + + >>> def export(tree_node, dom_node): + ... element = doc.createElement(u'element') + ... element.setAttribute(u'title', unicode(tree_node)) + ... dom_child = dom_node.appendChild(element) + ... for child in tree_node: + ... export(tree_node[child], dom_child) + +We can the export the document to XML: + + >>> from pprint import pprint + >>> export(tree, root) + >>> print doc.toxml() + <?xml version="1.0" ?> + <themes><element title="Theme('A Theme')"><element title="ThemePage('A Theme page')"><element title="PageBlock('Top block')"><element title="Cell('Left cell')"/><element title="Cell('Main cell')"/></element></element></element></themes> + + +[1] http://docs.python.org/lib/module-xml.dom.html Added: cpsskins/branches/paris-sprint-2006/tests/test_dom.py ============================================================================== --- (empty file) +++ cpsskins/branches/paris-sprint-2006/tests/test_dom.py Sat Apr 29 18:45:02 2006 @@ -0,0 +1,30 @@ +############################################################################## +# +# Copyright (c) 2005-2006 Nuxeo and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""DOM tests + +$Id$ +""" +__docformat__ = "reStructuredText" + +import unittest + +from zope.testing.doctestunit import DocFileSuite + +def test_suite(): + return unittest.TestSuite(( + DocFileSuite('../setup/DOM.txt'), + )) + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') -- http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins