~ubuntu-branches/debian/wheezy/lxml/wheezy

« back to all changes in this revision

Viewing changes to doc/html/objectify.html

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-10 11:32:29 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100310113229-nydaajy3eky89scu
Tags: 2.2.6-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
3
<head>
4
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
 
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
 
5
<meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
6
6
<title>lxml.objectify</title>
7
7
<meta name="authors" content="Stefan Behnel  Holger Joukl" />
8
8
<link rel="stylesheet" href="style.css" type="text/css" />
9
9
</head>
10
10
<body>
11
11
<div class="document" id="lxml-objectify">
12
 
<div class="sidemenu"><ul id="lxml-section"><li><span class="section title">lxml</span><ul class="menu foreign" id="index-menu"><li class="menu title"><a href="index.html">lxml</a><ul class="submenu"><li class="menu item"><a href="index.html#introduction">Introduction</a></li><li class="menu item"><a href="index.html#documentation">Documentation</a></li><li class="menu item"><a href="index.html#download">Download</a></li><li class="menu item"><a href="index.html#mailing-list">Mailing list</a></li><li class="menu item"><a href="index.html#bug-tracker">Bug tracker</a></li><li class="menu item"><a href="index.html#license">License</a></li><li class="menu item"><a href="index.html#old-versions">Old Versions</a></li></ul></li></ul><ul class="menu foreign" id="intro-menu"><li class="menu title"><a href="intro.html">Why lxml?</a><ul class="submenu"><li class="menu item"><a href="intro.html#motto">Motto</a></li><li class="menu item"><a href="intro.html#aims">Aims</a></li></ul></li></ul><ul class="menu foreign" id="installation-menu"><li class="menu title"><a href="installation.html">Installing lxml</a><ul class="submenu"><li class="menu item"><a href="installation.html#requirements">Requirements</a></li><li class="menu item"><a href="installation.html#installation">Installation</a></li><li class="menu item"><a href="installation.html#building-lxml-from-sources">Building lxml from sources</a></li><li class="menu item"><a href="installation.html#ms-windows">MS Windows</a></li><li class="menu item"><a href="installation.html#macos-x">MacOS-X</a></li></ul></li></ul><ul class="menu foreign" id="lxml2-menu"><li class="menu title"><a href="lxml2.html">What's new in lxml 2.0?</a><ul class="submenu"><li class="menu item"><a href="lxml2.html#changes-in-etree-and-objectify">Changes in etree and objectify</a></li><li class="menu item"><a href="lxml2.html#new-modules">New modules</a></li></ul></li></ul><ul class="menu foreign" id="performance-menu"><li class="menu title"><a href="performance.html">Benchmarks and Speed</a><ul class="submenu"><li class="menu item"><a href="performance.html#general-notes">General notes</a></li><li class="menu item"><a href="performance.html#how-to-read-the-timings">How to read the timings</a></li><li class="menu item"><a href="performance.html#parsing-and-serialising">Parsing and Serialising</a></li><li class="menu item"><a href="performance.html#the-elementtree-api">The ElementTree API</a></li><li class="menu item"><a href="performance.html#xpath">XPath</a></li><li class="menu item"><a href="performance.html#a-longer-example">A longer example</a></li><li class="menu item"><a href="performance.html#lxml-objectify">lxml.objectify</a></li></ul></li></ul><ul class="menu foreign" id="compatibility-menu"><li class="menu title"><a href="compatibility.html">ElementTree compatibility of lxml.etree</a></li></ul><ul class="menu foreign" id="FAQ-menu"><li class="menu title"><a href="FAQ.html">lxml FAQ - Frequently Asked Questions</a><ul class="submenu"><li class="menu item"><a href="FAQ.html#general-questions">General Questions</a></li><li class="menu item"><a href="FAQ.html#installation">Installation</a></li><li class="menu item"><a href="FAQ.html#contributing">Contributing</a></li><li class="menu item"><a href="FAQ.html#bugs">Bugs</a></li><li class="menu item"><a href="FAQ.html#threading">Threading</a></li><li class="menu item"><a href="FAQ.html#parsing-and-serialisation">Parsing and Serialisation</a></li><li class="menu item"><a href="FAQ.html#xpath-and-document-traversal">XPath and Document Traversal</a></li></ul></li></ul></li></ul><ul id="Developing with lxml-section"><li><span class="section title">Developing with lxml</span><ul class="menu foreign" id="tutorial-menu"><li class="menu title"><a href="tutorial.html">The lxml.etree Tutorial</a><ul class="submenu"><li class="menu item"><a href="tutorial.html#the-element-class">The Element class</a></li><li class="menu item"><a href="tutorial.html#the-elementtree-class">The ElementTree class</a></li><li class="menu item"><a href="tutorial.html#parsing-from-strings-and-files">Parsing from strings and files</a></li><li class="menu item"><a href="tutorial.html#namespaces">Namespaces</a></li><li class="menu item"><a href="tutorial.html#the-e-factory">The E-factory</a></li><li class="menu item"><a href="tutorial.html#elementpath">ElementPath</a></li></ul></li></ul><ul class="menu foreign" id="api index-menu"><li class="menu title"><a href="api/index.html">API reference</a></li></ul><ul class="menu foreign" id="api-menu"><li class="menu title"><a href="api.html">APIs specific to lxml.etree</a><ul class="submenu"><li class="menu item"><a href="api.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="api.html#other-element-apis">Other Element APIs</a></li><li class="menu item"><a href="api.html#trees-and-documents">Trees and Documents</a></li><li class="menu item"><a href="api.html#iteration">Iteration</a></li><li class="menu item"><a href="api.html#error-handling-on-exceptions">Error handling on exceptions</a></li><li class="menu item"><a href="api.html#error-logging">Error logging</a></li><li class="menu item"><a href="api.html#serialisation">Serialisation</a></li><li class="menu item"><a href="api.html#cdata">CDATA</a></li><li class="menu item"><a href="api.html#xinclude-and-elementinclude">XInclude and ElementInclude</a></li><li class="menu item"><a href="api.html#write-c14n-on-elementtree">write_c14n on ElementTree</a></li></ul></li></ul><ul class="menu foreign" id="parsing-menu"><li class="menu title"><a href="parsing.html">Parsing XML and HTML with lxml</a><ul class="submenu"><li class="menu item"><a href="parsing.html#parsers">Parsers</a></li><li class="menu item"><a href="parsing.html#the-target-parser-interface">The target parser interface</a></li><li class="menu item"><a href="parsing.html#the-feed-parser-interface">The feed parser interface</a></li><li class="menu item"><a href="parsing.html#iterparse-and-iterwalk">iterparse and iterwalk</a></li><li class="menu item"><a href="parsing.html#python-unicode-strings">Python unicode strings</a></li></ul></li></ul><ul class="menu foreign" id="validation-menu"><li class="menu title"><a href="validation.html">Validation with lxml</a><ul class="submenu"><li class="menu item"><a href="validation.html#validation-at-parse-time">Validation at parse time</a></li><li class="menu item"><a href="validation.html#dtd">DTD</a></li><li class="menu item"><a href="validation.html#relaxng">RelaxNG</a></li><li class="menu item"><a href="validation.html#xmlschema">XMLSchema</a></li><li class="menu item"><a href="validation.html#schematron">Schematron</a></li></ul></li></ul><ul class="menu foreign" id="xpathxslt-menu"><li class="menu title"><a href="xpathxslt.html">XPath and XSLT with lxml</a><ul class="submenu"><li class="menu item"><a href="xpathxslt.html#xpath">XPath</a></li><li class="menu item"><a href="xpathxslt.html#xslt">XSLT</a></li></ul></li></ul><ul class="menu current" id="objectify-menu"><li class="menu title"><a href="objectify.html">lxml.objectify</a><ul class="submenu"><li class="menu item"><a href="objectify.html#the-lxml-objectify-api">The lxml.objectify API</a></li><li class="menu item"><a href="objectify.html#asserting-a-schema">Asserting a Schema</a></li><li class="menu item"><a href="objectify.html#objectpath">ObjectPath</a></li><li class="menu item"><a href="objectify.html#python-data-types">Python data types</a></li><li class="menu item"><a href="objectify.html#how-data-types-are-matched">How data types are matched</a></li><li class="menu item"><a href="objectify.html#what-is-different-from-lxml-etree">What is different from lxml.etree?</a></li></ul></li></ul><ul class="menu foreign" id="lxmlhtml-menu"><li class="menu title"><a href="lxmlhtml.html">lxml.html</a><ul class="submenu"><li class="menu item"><a href="lxmlhtml.html#parsing-html">Parsing HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-element-methods">HTML Element Methods</a></li><li class="menu item"><a href="lxmlhtml.html#running-html-doctests">Running HTML doctests</a></li><li class="menu item"><a href="lxmlhtml.html#creating-html-with-the-e-factory">Creating HTML with the E-factory</a></li><li class="menu item"><a href="lxmlhtml.html#working-with-links">Working with links</a></li><li class="menu item"><a href="lxmlhtml.html#forms">Forms</a></li><li class="menu item"><a href="lxmlhtml.html#cleaning-up-html">Cleaning up HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-diff">HTML Diff</a></li><li class="menu item"><a href="lxmlhtml.html#examples">Examples</a></li></ul></li></ul><ul class="menu foreign" id="cssselect-menu"><li class="menu title"><a href="cssselect.html">lxml.cssselect</a><ul class="submenu"><li class="menu item"><a href="cssselect.html#the-cssselector-class">The CSSSelector class</a></li><li class="menu item"><a href="cssselect.html#css-selectors">CSS Selectors</a></li><li class="menu item"><a href="cssselect.html#namespaces">Namespaces</a></li><li class="menu item"><a href="cssselect.html#limitations">Limitations</a></li></ul></li></ul><ul class="menu foreign" id="elementsoup-menu"><li class="menu title"><a href="elementsoup.html">BeautifulSoup Parser</a><ul class="submenu"><li class="menu item"><a href="elementsoup.html#parsing-with-the-soupparser">Parsing with the soupparser</a></li><li class="menu item"><a href="elementsoup.html#entity-handling">Entity handling</a></li><li class="menu item"><a href="elementsoup.html#using-soupparser-as-a-fallback">Using soupparser as a fallback</a></li><li class="menu item"><a href="elementsoup.html#using-only-the-encoding-detection">Using only the encoding detection</a></li></ul></li></ul><ul class="menu foreign" id="html5parser-menu"><li class="menu title"><a href="html5parser.html">html5lib Parser</a><ul class="submenu"><li class="menu item"><a href="html5parser.html#differences-to-regular-html-parsing">Differences to regular HTML parsing</a></li><li class="menu item"><a href="html5parser.html#function-reference">Function Reference</a></li></ul></li></ul></li></ul><ul id="Extending lxml-section"><li><span class="section title">Extending lxml</span><ul class="menu foreign" id="resolvers-menu"><li class="menu title"><a href="resolvers.html">Document loading and URL resolving</a><ul class="submenu"><li class="menu item"><a href="resolvers.html#uri-resolvers">URI Resolvers</a></li><li class="menu item"><a href="resolvers.html#document-loading-in-context">Document loading in context</a></li><li class="menu item"><a href="resolvers.html#i-o-access-control-in-xslt">I/O access control in XSLT</a></li></ul></li></ul><ul class="menu foreign" id="extensions-menu"><li class="menu title"><a href="extensions.html">Python extensions for XPath and XSLT</a><ul class="submenu"><li class="menu item"><a href="extensions.html#xpath-extension-functions">XPath Extension functions</a></li><li class="menu item"><a href="extensions.html#xslt-extension-elements">XSLT extension elements</a></li></ul></li></ul><ul class="menu foreign" id="element classes-menu"><li class="menu title"><a href="element_classes.html">Using custom Element classes in lxml</a><ul class="submenu"><li class="menu item"><a href="element_classes.html#background-on-element-proxies">Background on Element proxies</a></li><li class="menu item"><a href="element_classes.html#element-initialization">Element initialization</a></li><li class="menu item"><a href="element_classes.html#setting-up-a-class-lookup-scheme">Setting up a class lookup scheme</a></li><li class="menu item"><a href="element_classes.html#generating-xml-with-custom-classes">Generating XML with custom classes</a></li><li class="menu item"><a href="element_classes.html#implementing-namespaces">Implementing namespaces</a></li></ul></li></ul><ul class="menu foreign" id="sax-menu"><li class="menu title"><a href="sax.html">Sax support</a><ul class="submenu"><li class="menu item"><a href="sax.html#building-a-tree-from-sax-events">Building a tree from SAX events</a></li><li class="menu item"><a href="sax.html#producing-sax-events-from-an-elementtree-or-element">Producing SAX events from an ElementTree or Element</a></li><li class="menu item"><a href="sax.html#interfacing-with-pulldom-minidom">Interfacing with pulldom/minidom</a></li></ul></li></ul><ul class="menu foreign" id="capi-menu"><li class="menu title"><a href="capi.html">The public C-API of lxml.etree</a><ul class="submenu"><li class="menu item"><a href="capi.html#writing-external-modules-in-cython">Writing external modules in Cython</a></li><li class="menu item"><a href="capi.html#writing-external-modules-in-c">Writing external modules in C</a></li></ul></li></ul></li></ul><ul id="Developing lxml-section"><li><span class="section title">Developing lxml</span><ul class="menu foreign" id="build-menu"><li class="menu title"><a href="build.html">How to build lxml from source</a><ul class="submenu"><li class="menu item"><a href="build.html#cython">Cython</a></li><li class="menu item"><a href="build.html#subversion">Subversion</a></li><li class="menu item"><a href="build.html#setuptools">Setuptools</a></li><li class="menu item"><a href="build.html#running-the-tests-and-reporting-errors">Running the tests and reporting errors</a></li><li class="menu item"><a href="build.html#building-an-egg">Building an egg</a></li><li class="menu item"><a href="build.html#building-lxml-on-macos-x">Building lxml on MacOS-X</a></li><li class="menu item"><a href="build.html#static-linking-on-windows">Static linking on Windows</a></li><li class="menu item"><a href="build.html#building-debian-packages-from-svn-sources">Building Debian packages from SVN sources</a></li></ul></li></ul><ul class="menu foreign" id="lxml source howto-menu"><li class="menu title"><a href="lxml-source-howto.html">How to read the source of lxml</a><ul class="submenu"><li class="menu item"><a href="lxml-source-howto.html#what-is-cython">What is Cython?</a></li><li class="menu item"><a href="lxml-source-howto.html#where-to-start">Where to start?</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="lxml-source-howto.html#python-modules">Python modules</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-objectify">lxml.objectify</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-html">lxml.html</a></li></ul></li></ul><ul class="menu foreign" id="changes 2 2 4-menu"><li class="menu title"><a href="changes-2.2.4.html">Release Changelog</a></li></ul><ul class="menu foreign" id="credits-menu"><li class="menu title"><a href="credits.html">Credits</a><ul class="submenu"><li class="menu item"><a href="credits.html#main-contributors">Main contributors</a></li><li class="menu item"><a href="credits.html#special-thanks-goes-to">Special thanks goes to:</a></li></ul></li></ul></li></ul></div><h1 class="title">lxml.objectify</h1>
 
12
<div class="sidemenu"><ul id="lxml-section"><li><span class="section title">lxml</span><ul class="menu foreign" id="index-menu"><li class="menu title"><a href="index.html">lxml</a><ul class="submenu"><li class="menu item"><a href="index.html#introduction">Introduction</a></li><li class="menu item"><a href="index.html#documentation">Documentation</a></li><li class="menu item"><a href="index.html#download">Download</a></li><li class="menu item"><a href="index.html#mailing-list">Mailing list</a></li><li class="menu item"><a href="index.html#bug-tracker">Bug tracker</a></li><li class="menu item"><a href="index.html#license">License</a></li><li class="menu item"><a href="index.html#old-versions">Old Versions</a></li></ul></li></ul><ul class="menu foreign" id="intro-menu"><li class="menu title"><a href="intro.html">Why lxml?</a><ul class="submenu"><li class="menu item"><a href="intro.html#motto">Motto</a></li><li class="menu item"><a href="intro.html#aims">Aims</a></li></ul></li></ul><ul class="menu foreign" id="installation-menu"><li class="menu title"><a href="installation.html">Installing lxml</a><ul class="submenu"><li class="menu item"><a href="installation.html#requirements">Requirements</a></li><li class="menu item"><a href="installation.html#installation">Installation</a></li><li class="menu item"><a href="installation.html#installation-in-activepython">Installation in ActivePython</a></li><li class="menu item"><a href="installation.html#building-lxml-from-sources">Building lxml from sources</a></li><li class="menu item"><a href="installation.html#ms-windows">MS Windows</a></li><li class="menu item"><a href="installation.html#macos-x">MacOS-X</a></li></ul></li></ul><ul class="menu foreign" id="lxml2-menu"><li class="menu title"><a href="lxml2.html">What's new in lxml 2.0?</a><ul class="submenu"><li class="menu item"><a href="lxml2.html#changes-in-etree-and-objectify">Changes in etree and objectify</a></li><li class="menu item"><a href="lxml2.html#new-modules">New modules</a></li></ul></li></ul><ul class="menu foreign" id="performance-menu"><li class="menu title"><a href="performance.html">Benchmarks and Speed</a><ul class="submenu"><li class="menu item"><a href="performance.html#general-notes">General notes</a></li><li class="menu item"><a href="performance.html#how-to-read-the-timings">How to read the timings</a></li><li class="menu item"><a href="performance.html#parsing-and-serialising">Parsing and Serialising</a></li><li class="menu item"><a href="performance.html#the-elementtree-api">The ElementTree API</a></li><li class="menu item"><a href="performance.html#xpath">XPath</a></li><li class="menu item"><a href="performance.html#a-longer-example">A longer example</a></li><li class="menu item"><a href="performance.html#lxml-objectify">lxml.objectify</a></li></ul></li></ul><ul class="menu foreign" id="compatibility-menu"><li class="menu title"><a href="compatibility.html">ElementTree compatibility of lxml.etree</a></li></ul><ul class="menu foreign" id="FAQ-menu"><li class="menu title"><a href="FAQ.html">lxml FAQ - Frequently Asked Questions</a><ul class="submenu"><li class="menu item"><a href="FAQ.html#general-questions">General Questions</a></li><li class="menu item"><a href="FAQ.html#installation">Installation</a></li><li class="menu item"><a href="FAQ.html#contributing">Contributing</a></li><li class="menu item"><a href="FAQ.html#bugs">Bugs</a></li><li class="menu item"><a href="FAQ.html#threading">Threading</a></li><li class="menu item"><a href="FAQ.html#parsing-and-serialisation">Parsing and Serialisation</a></li><li class="menu item"><a href="FAQ.html#xpath-and-document-traversal">XPath and Document Traversal</a></li></ul></li></ul></li></ul><ul id="Developing with lxml-section"><li><span class="section title">Developing with lxml</span><ul class="menu foreign" id="tutorial-menu"><li class="menu title"><a href="tutorial.html">The lxml.etree Tutorial</a><ul class="submenu"><li class="menu item"><a href="tutorial.html#the-element-class">The Element class</a></li><li class="menu item"><a href="tutorial.html#the-elementtree-class">The ElementTree class</a></li><li class="menu item"><a href="tutorial.html#parsing-from-strings-and-files">Parsing from strings and files</a></li><li class="menu item"><a href="tutorial.html#namespaces">Namespaces</a></li><li class="menu item"><a href="tutorial.html#the-e-factory">The E-factory</a></li><li class="menu item"><a href="tutorial.html#elementpath">ElementPath</a></li></ul></li></ul><ul class="menu foreign" id="api index-menu"><li class="menu title"><a href="api/index.html">API reference</a></li></ul><ul class="menu foreign" id="api-menu"><li class="menu title"><a href="api.html">APIs specific to lxml.etree</a><ul class="submenu"><li class="menu item"><a href="api.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="api.html#other-element-apis">Other Element APIs</a></li><li class="menu item"><a href="api.html#trees-and-documents">Trees and Documents</a></li><li class="menu item"><a href="api.html#iteration">Iteration</a></li><li class="menu item"><a href="api.html#error-handling-on-exceptions">Error handling on exceptions</a></li><li class="menu item"><a href="api.html#error-logging">Error logging</a></li><li class="menu item"><a href="api.html#serialisation">Serialisation</a></li><li class="menu item"><a href="api.html#cdata">CDATA</a></li><li class="menu item"><a href="api.html#xinclude-and-elementinclude">XInclude and ElementInclude</a></li><li class="menu item"><a href="api.html#write-c14n-on-elementtree">write_c14n on ElementTree</a></li></ul></li></ul><ul class="menu foreign" id="parsing-menu"><li class="menu title"><a href="parsing.html">Parsing XML and HTML with lxml</a><ul class="submenu"><li class="menu item"><a href="parsing.html#parsers">Parsers</a></li><li class="menu item"><a href="parsing.html#the-target-parser-interface">The target parser interface</a></li><li class="menu item"><a href="parsing.html#the-feed-parser-interface">The feed parser interface</a></li><li class="menu item"><a href="parsing.html#iterparse-and-iterwalk">iterparse and iterwalk</a></li><li class="menu item"><a href="parsing.html#python-unicode-strings">Python unicode strings</a></li></ul></li></ul><ul class="menu foreign" id="validation-menu"><li class="menu title"><a href="validation.html">Validation with lxml</a><ul class="submenu"><li class="menu item"><a href="validation.html#validation-at-parse-time">Validation at parse time</a></li><li class="menu item"><a href="validation.html#dtd">DTD</a></li><li class="menu item"><a href="validation.html#relaxng">RelaxNG</a></li><li class="menu item"><a href="validation.html#xmlschema">XMLSchema</a></li><li class="menu item"><a href="validation.html#schematron">Schematron</a></li></ul></li></ul><ul class="menu foreign" id="xpathxslt-menu"><li class="menu title"><a href="xpathxslt.html">XPath and XSLT with lxml</a><ul class="submenu"><li class="menu item"><a href="xpathxslt.html#xpath">XPath</a></li><li class="menu item"><a href="xpathxslt.html#xslt">XSLT</a></li></ul></li></ul><ul class="menu current" id="objectify-menu"><li class="menu title"><a href="objectify.html">lxml.objectify</a><ul class="submenu"><li class="menu item"><a href="objectify.html#the-lxml-objectify-api">The lxml.objectify API</a></li><li class="menu item"><a href="objectify.html#asserting-a-schema">Asserting a Schema</a></li><li class="menu item"><a href="objectify.html#objectpath">ObjectPath</a></li><li class="menu item"><a href="objectify.html#python-data-types">Python data types</a></li><li class="menu item"><a href="objectify.html#how-data-types-are-matched">How data types are matched</a></li><li class="menu item"><a href="objectify.html#what-is-different-from-lxml-etree">What is different from lxml.etree?</a></li></ul></li></ul><ul class="menu foreign" id="lxmlhtml-menu"><li class="menu title"><a href="lxmlhtml.html">lxml.html</a><ul class="submenu"><li class="menu item"><a href="lxmlhtml.html#parsing-html">Parsing HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-element-methods">HTML Element Methods</a></li><li class="menu item"><a href="lxmlhtml.html#running-html-doctests">Running HTML doctests</a></li><li class="menu item"><a href="lxmlhtml.html#creating-html-with-the-e-factory">Creating HTML with the E-factory</a></li><li class="menu item"><a href="lxmlhtml.html#working-with-links">Working with links</a></li><li class="menu item"><a href="lxmlhtml.html#forms">Forms</a></li><li class="menu item"><a href="lxmlhtml.html#cleaning-up-html">Cleaning up HTML</a></li><li class="menu item"><a href="lxmlhtml.html#html-diff">HTML Diff</a></li><li class="menu item"><a href="lxmlhtml.html#examples">Examples</a></li></ul></li></ul><ul class="menu foreign" id="cssselect-menu"><li class="menu title"><a href="cssselect.html">lxml.cssselect</a><ul class="submenu"><li class="menu item"><a href="cssselect.html#the-cssselector-class">The CSSSelector class</a></li><li class="menu item"><a href="cssselect.html#css-selectors">CSS Selectors</a></li><li class="menu item"><a href="cssselect.html#namespaces">Namespaces</a></li><li class="menu item"><a href="cssselect.html#limitations">Limitations</a></li></ul></li></ul><ul class="menu foreign" id="elementsoup-menu"><li class="menu title"><a href="elementsoup.html">BeautifulSoup Parser</a><ul class="submenu"><li class="menu item"><a href="elementsoup.html#parsing-with-the-soupparser">Parsing with the soupparser</a></li><li class="menu item"><a href="elementsoup.html#entity-handling">Entity handling</a></li><li class="menu item"><a href="elementsoup.html#using-soupparser-as-a-fallback">Using soupparser as a fallback</a></li><li class="menu item"><a href="elementsoup.html#using-only-the-encoding-detection">Using only the encoding detection</a></li></ul></li></ul><ul class="menu foreign" id="html5parser-menu"><li class="menu title"><a href="html5parser.html">html5lib Parser</a><ul class="submenu"><li class="menu item"><a href="html5parser.html#differences-to-regular-html-parsing">Differences to regular HTML parsing</a></li><li class="menu item"><a href="html5parser.html#function-reference">Function Reference</a></li></ul></li></ul></li></ul><ul id="Extending lxml-section"><li><span class="section title">Extending lxml</span><ul class="menu foreign" id="resolvers-menu"><li class="menu title"><a href="resolvers.html">Document loading and URL resolving</a><ul class="submenu"><li class="menu item"><a href="resolvers.html#uri-resolvers">URI Resolvers</a></li><li class="menu item"><a href="resolvers.html#document-loading-in-context">Document loading in context</a></li><li class="menu item"><a href="resolvers.html#i-o-access-control-in-xslt">I/O access control in XSLT</a></li></ul></li></ul><ul class="menu foreign" id="extensions-menu"><li class="menu title"><a href="extensions.html">Python extensions for XPath and XSLT</a><ul class="submenu"><li class="menu item"><a href="extensions.html#xpath-extension-functions">XPath Extension functions</a></li><li class="menu item"><a href="extensions.html#xslt-extension-elements">XSLT extension elements</a></li></ul></li></ul><ul class="menu foreign" id="element classes-menu"><li class="menu title"><a href="element_classes.html">Using custom Element classes in lxml</a><ul class="submenu"><li class="menu item"><a href="element_classes.html#background-on-element-proxies">Background on Element proxies</a></li><li class="menu item"><a href="element_classes.html#element-initialization">Element initialization</a></li><li class="menu item"><a href="element_classes.html#setting-up-a-class-lookup-scheme">Setting up a class lookup scheme</a></li><li class="menu item"><a href="element_classes.html#generating-xml-with-custom-classes">Generating XML with custom classes</a></li><li class="menu item"><a href="element_classes.html#implementing-namespaces">Implementing namespaces</a></li></ul></li></ul><ul class="menu foreign" id="sax-menu"><li class="menu title"><a href="sax.html">Sax support</a><ul class="submenu"><li class="menu item"><a href="sax.html#building-a-tree-from-sax-events">Building a tree from SAX events</a></li><li class="menu item"><a href="sax.html#producing-sax-events-from-an-elementtree-or-element">Producing SAX events from an ElementTree or Element</a></li><li class="menu item"><a href="sax.html#interfacing-with-pulldom-minidom">Interfacing with pulldom/minidom</a></li></ul></li></ul><ul class="menu foreign" id="capi-menu"><li class="menu title"><a href="capi.html">The public C-API of lxml.etree</a><ul class="submenu"><li class="menu item"><a href="capi.html#writing-external-modules-in-cython">Writing external modules in Cython</a></li><li class="menu item"><a href="capi.html#writing-external-modules-in-c">Writing external modules in C</a></li></ul></li></ul></li></ul><ul id="Developing lxml-section"><li><span class="section title">Developing lxml</span><ul class="menu foreign" id="build-menu"><li class="menu title"><a href="build.html">How to build lxml from source</a><ul class="submenu"><li class="menu item"><a href="build.html#cython">Cython</a></li><li class="menu item"><a href="build.html#subversion">Subversion</a></li><li class="menu item"><a href="build.html#setuptools">Setuptools</a></li><li class="menu item"><a href="build.html#running-the-tests-and-reporting-errors">Running the tests and reporting errors</a></li><li class="menu item"><a href="build.html#building-an-egg">Building an egg</a></li><li class="menu item"><a href="build.html#building-lxml-on-macos-x">Building lxml on MacOS-X</a></li><li class="menu item"><a href="build.html#static-linking-on-windows">Static linking on Windows</a></li><li class="menu item"><a href="build.html#building-debian-packages-from-svn-sources">Building Debian packages from SVN sources</a></li></ul></li></ul><ul class="menu foreign" id="lxml source howto-menu"><li class="menu title"><a href="lxml-source-howto.html">How to read the source of lxml</a><ul class="submenu"><li class="menu item"><a href="lxml-source-howto.html#what-is-cython">What is Cython?</a></li><li class="menu item"><a href="lxml-source-howto.html#where-to-start">Where to start?</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-etree">lxml.etree</a></li><li class="menu item"><a href="lxml-source-howto.html#python-modules">Python modules</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-objectify">lxml.objectify</a></li><li class="menu item"><a href="lxml-source-howto.html#lxml-html">lxml.html</a></li></ul></li></ul><ul class="menu foreign" id="changes 2 2 6-menu"><li class="menu title"><a href="changes-2.2.6.html">Release Changelog</a></li></ul><ul class="menu foreign" id="credits-menu"><li class="menu title"><a href="credits.html">Credits</a><ul class="submenu"><li class="menu item"><a href="credits.html#main-contributors">Main contributors</a></li><li class="menu item"><a href="credits.html#special-thanks-goes-to">Special thanks goes to:</a></li></ul></li></ul></li></ul></div><h1 class="title">lxml.objectify</h1>
13
13
<table class="docinfo" frame="void" rules="none">
14
14
<col class="docinfo-name" />
15
15
<col class="docinfo-content" />
56
56
<li><a class="reference internal" href="#what-is-different-from-lxml-etree" id="id17">What is different from lxml.etree?</a></li>
57
57
</ul>
58
58
</div>
59
 
<p>To set up and use <tt class="docutils literal"><span class="pre">objectify</span></tt>, you need both the <tt class="docutils literal"><span class="pre">lxml.etree</span></tt>
60
 
module and <tt class="docutils literal"><span class="pre">lxml.objectify</span></tt>:</p>
 
59
<p>To set up and use <tt class="docutils literal">objectify</tt>, you need both the <tt class="docutils literal">lxml.etree</tt>
 
60
module and <tt class="docutils literal">lxml.objectify</tt>:</p>
61
61
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">etree</span>
62
62
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">objectify</span>
63
63
</pre></div>
64
64
<p>The objectify API is very different from the ElementTree API.  If it
65
65
is used, it should not be mixed with other element implementations
66
 
(such as trees parsed with <tt class="docutils literal"><span class="pre">lxml.etree</span></tt>), to avoid non-obvious
 
66
(such as trees parsed with <tt class="docutils literal">lxml.etree</tt>), to avoid non-obvious
67
67
behaviour.</p>
68
68
<p>The <a class="reference external" href="performance.html#lxml-objectify">benchmark page</a> has some hints on performance optimisation of
69
69
code using lxml.objectify.</p>
75
75
formatting of XML output.</p>
76
76
<div class="section" id="the-lxml-objectify-api">
77
77
<h1>The lxml.objectify API</h1>
78
 
<p>In <tt class="docutils literal"><span class="pre">lxml.objectify</span></tt>, element trees provide an API that models the behaviour
 
78
<p>In <tt class="docutils literal">lxml.objectify</tt>, element trees provide an API that models the behaviour
79
79
of normal Python object trees as closely as possible.</p>
80
80
<div class="section" id="creating-objectify-trees">
81
81
<h2>Creating objectify trees</h2>
82
 
<p>As with <tt class="docutils literal"><span class="pre">lxml.etree</span></tt>, you can either create an <tt class="docutils literal"><span class="pre">objectify</span></tt> tree by
 
82
<p>As with <tt class="docutils literal">lxml.etree</tt>, you can either create an <tt class="docutils literal">objectify</tt> tree by
83
83
parsing an XML document or by building one from scratch.  To parse a
84
 
document, just use the <tt class="docutils literal"><span class="pre">parse()</span></tt> or <tt class="docutils literal"><span class="pre">fromstring()</span></tt> functions of
 
84
document, just use the <tt class="docutils literal">parse()</tt> or <tt class="docutils literal">fromstring()</tt> functions of
85
85
the module:</p>
86
86
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">fileobject</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">'&lt;test/&gt;'</span><span class="p">)</span>
87
87
 
93
93
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">objectify</span><span class="o">.</span><span class="n">ObjectifiedElement</span><span class="p">))</span>
94
94
<span class="go">True</span>
95
95
</pre></div>
96
 
<p>To build a new tree in memory, <tt class="docutils literal"><span class="pre">objectify</span></tt> replicates the standard
97
 
factory function <tt class="docutils literal"><span class="pre">Element()</span></tt> from <tt class="docutils literal"><span class="pre">lxml.etree</span></tt>:</p>
 
96
<p>To build a new tree in memory, <tt class="docutils literal">objectify</tt> replicates the standard
 
97
factory function <tt class="docutils literal">Element()</tt> from <tt class="docutils literal">lxml.etree</tt>:</p>
98
98
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">obj_el</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s">"new"</span><span class="p">)</span>
99
99
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">obj_el</span><span class="p">,</span> <span class="n">objectify</span><span class="o">.</span><span class="n">ObjectifiedElement</span><span class="p">))</span>
100
100
<span class="go">True</span>
105
105
</pre></div>
106
106
<p>New subelements will automatically inherit the objectify behaviour
107
107
from their tree.  However, all independent elements that you create
108
 
through the <tt class="docutils literal"><span class="pre">Element()</span></tt> factory of lxml.etree (instead of objectify)
109
 
will not support the <tt class="docutils literal"><span class="pre">objectify</span></tt> API by themselves:</p>
 
108
through the <tt class="docutils literal">Element()</tt> factory of lxml.etree (instead of objectify)
 
109
will not support the <tt class="docutils literal">objectify</tt> API by themselves:</p>
110
110
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subel</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">obj_el</span><span class="p">,</span> <span class="s">"sub"</span><span class="p">)</span>
111
111
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">subel</span><span class="p">,</span> <span class="n">objectify</span><span class="o">.</span><span class="n">ObjectifiedElement</span><span class="p">))</span>
112
112
<span class="go">True</span>
118
118
</div>
119
119
<div class="section" id="element-access-through-object-attributes">
120
120
<h2>Element access through object attributes</h2>
121
 
<p>The main idea behind the <tt class="docutils literal"><span class="pre">objectify</span></tt> API is to hide XML element access
 
121
<p>The main idea behind the <tt class="docutils literal">objectify</tt> API is to hide XML element access
122
122
behind the usual object attribute access pattern.  Asking an element for an
123
123
attribute will return the sequence of children with corresponding tag names:</p>
124
124
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s">"root"</span><span class="p">)</span>
161
161
<span class="go">['x', 'x']</span>
162
162
</pre></div>
163
163
<p>If you want to iterate over all children or need to provide a specific
164
 
namespace for the tag, use the <tt class="docutils literal"><span class="pre">iterchildren()</span></tt> method.  Like the other
 
164
namespace for the tag, use the <tt class="docutils literal">iterchildren()</tt> method.  Like the other
165
165
methods for iteration, it supports an optional tag keyword argument:</p>
166
166
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span> <span class="n">el</span><span class="o">.</span><span class="n">tag</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">root</span><span class="o">.</span><span class="n">iterchildren</span><span class="p">()</span> <span class="p">]</span>
167
167
<span class="go">['b', 'x', 'x']</span>
264
264
<span class="go">  &lt;how-many py:pytype="int"&gt;5&lt;/how-many&gt;</span>
265
265
<span class="go">&lt;/root&gt;</span>
266
266
</pre></div>
267
 
<p><tt class="docutils literal"><span class="pre">objectify.E</span></tt> is an instance of <tt class="docutils literal"><span class="pre">objectify.ElementMaker</span></tt>.  By default, it
 
267
<p><tt class="docutils literal">objectify.E</tt> is an instance of <tt class="docutils literal">objectify.ElementMaker</tt>.  By default, it
268
268
creates pytype annotated Elements without a namespace.  You can switch off the
269
 
pytype annotation by passing False to the <tt class="docutils literal"><span class="pre">annotate</span></tt> keyword argument of the
270
 
constructor.  You can also pass a default namespace and an <tt class="docutils literal"><span class="pre">nsmap</span></tt>:</p>
 
269
pytype annotation by passing False to the <tt class="docutils literal">annotate</tt> keyword argument of the
 
270
constructor.  You can also pass a default namespace and an <tt class="docutils literal">nsmap</tt>:</p>
271
271
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">myE</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">ElementMaker</span><span class="p">(</span><span class="n">annotate</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
272
272
<span class="gp">... </span>          <span class="n">namespace</span><span class="o">=</span><span class="s">"http://my/ns"</span><span class="p">,</span> <span class="n">nsmap</span><span class="o">=</span><span class="p">{</span><span class="bp">None</span> <span class="p">:</span> <span class="s">"http://my/ns"</span><span class="p">})</span>
273
273
 
291
291
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">b</span><span class="o">.</span><span class="n">tag</span><span class="p">)</span>
292
292
<span class="go">{http://ns/}b</span>
293
293
</pre></div>
294
 
<p>Note that the <tt class="docutils literal"><span class="pre">SubElement()</span></tt> factory of <tt class="docutils literal"><span class="pre">lxml.etree</span></tt> does not
 
294
<p>Note that the <tt class="docutils literal">SubElement()</tt> factory of <tt class="docutils literal">lxml.etree</tt> does not
295
295
inherit any namespaces when creating a new subelement.  Element
296
296
creation must be explicit about the namespace, and is simplified
297
297
through the E-factory as described above.</p>
305
305
<span class="nc">AttributeError: no such child</span>: <span class="n-Identifier">{http://ns/}c</span>
306
306
</pre></div>
307
307
<p>To access an element in a different namespace than its parent, you can
308
 
use <tt class="docutils literal"><span class="pre">getattr()</span></tt>:</p>
 
308
use <tt class="docutils literal">getattr()</tt>:</p>
309
309
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">"{http://other/}c"</span><span class="p">)</span>
310
310
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">tag</span><span class="p">)</span>
311
311
<span class="go">{http://other/}c</span>
376
376
<span class="gp">&gt;&gt;&gt; </span><span class="n">schema</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLSchema</span><span class="p">(</span><span class="nb">file</span><span class="o">=</span><span class="n">f</span><span class="p">)</span>
377
377
</pre></div>
378
378
<p>When creating the validating parser, we must make sure it <a class="reference external" href="#advance-element-class-lookup">returns
379
 
objectify trees</a>.  This is best done with the <tt class="docutils literal"><span class="pre">makeparser()</span></tt>
 
379
objectify trees</a>.  This is best done with the <tt class="docutils literal">makeparser()</tt>
380
380
function:</p>
381
381
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">makeparser</span><span class="p">(</span><span class="n">schema</span> <span class="o">=</span> <span class="n">schema</span><span class="p">)</span>
382
382
</pre></div>
399
399
<div class="section" id="objectpath">
400
400
<h1>ObjectPath</h1>
401
401
<p>For both convenience and speed, objectify supports its own path language,
402
 
represented by the <tt class="docutils literal"><span class="pre">ObjectPath</span></tt> class:</p>
 
402
represented by the <tt class="docutils literal">ObjectPath</tt> class:</p>
403
403
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s">"{http://ns/}root"</span><span class="p">)</span>
404
404
<span class="gp">&gt;&gt;&gt; </span><span class="n">b1</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="s">"{http://ns/}b"</span><span class="p">)</span>
405
405
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>  <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">b1</span><span class="p">,</span>   <span class="s">"{http://ns/}c"</span><span class="p">)</span>
505
505
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span> <span class="n">el</span><span class="o">.</span><span class="n">text</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">root</span><span class="p">)</span> <span class="p">]</span>
506
506
<span class="go">['my value', 'my new value']</span>
507
507
</pre></div>
508
 
<p>As with attribute assignment, <tt class="docutils literal"><span class="pre">setattr()</span></tt> accepts lists:</p>
 
508
<p>As with attribute assignment, <tt class="docutils literal">setattr()</tt> accepts lists:</p>
509
509
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">path</span><span class="o">.</span><span class="n">setattr</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="p">[</span><span class="s">"v1"</span><span class="p">,</span> <span class="s">"v2"</span><span class="p">,</span> <span class="s">"v3"</span><span class="p">])</span>
510
510
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span> <span class="n">el</span><span class="o">.</span><span class="n">text</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">path</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">root</span><span class="p">)</span> <span class="p">]</span>
511
511
<span class="go">['v1', 'v2', 'v3']</span>
519
519
  <span class="c">...</span>
520
520
<span class="nc">TypeError</span>: <span class="n-Identifier">creating indexed path attributes is not supported</span>
521
521
</pre></div>
522
 
<p>It is worth noting that ObjectPath does not depend on the <tt class="docutils literal"><span class="pre">objectify</span></tt> module
 
522
<p>It is worth noting that ObjectPath does not depend on the <tt class="docutils literal">objectify</tt> module
523
523
or the ObjectifiedElement implementation.  It can also be used in combination
524
524
with Elements from the normal lxml.etree API.</p>
525
525
</div>
556
556
<span class="go">1234 - 12345</span>
557
557
</pre></div>
558
558
<p>However, data elements continue to provide the objectify API.  This means that
559
 
sequence operations such as <tt class="docutils literal"><span class="pre">len()</span></tt>, slicing and indexing (e.g. of strings)
 
559
sequence operations such as <tt class="docutils literal">len()</tt>, slicing and indexing (e.g. of strings)
560
560
cannot behave as the Python types.  Like all other tree elements, they show
561
561
the normal slicing behaviour of objectify elements:</p>
562
562
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"&lt;root&gt;&lt;a&gt;test&lt;/a&gt;&lt;b&gt;toast&lt;/b&gt;&lt;/root&gt;"</span><span class="p">)</span>
576
576
</pre></div>
577
577
<p>If you need to run sequence operations on data types, you must ask the API for
578
578
the <em>real</em> Python value.  The string value is always available through the
579
 
normal ElementTree <tt class="docutils literal"><span class="pre">.text</span></tt> attribute.  Additionally, all data classes
580
 
provide a <tt class="docutils literal"><span class="pre">.pyval</span></tt> attribute that returns the value as plain Python type:</p>
 
579
normal ElementTree <tt class="docutils literal">.text</tt> attribute.  Additionally, all data classes
 
580
provide a <tt class="docutils literal">.pyval</tt> attribute that returns the value as plain Python type:</p>
581
581
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"&lt;root&gt;&lt;a&gt;test&lt;/a&gt;&lt;b&gt;5&lt;/b&gt;&lt;/root&gt;"</span><span class="p">)</span>
582
582
<span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">text</span>
583
583
<span class="go">'test'</span>
607
607
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">pyval</span><span class="p">)</span>
608
608
<span class="go">25</span>
609
609
</pre></div>
610
 
<p>In other words, <tt class="docutils literal"><span class="pre">objectify</span></tt> data elements behave like immutable Python
 
610
<p>In other words, <tt class="docutils literal">objectify</tt> data elements behave like immutable Python
611
611
types.  You can replace them, but not modify them.</p>
612
612
<div class="section" id="recursive-tree-dump">
613
613
<h2>Recursive tree dump</h2>
614
614
<p>To see the data types that are currently used, you can call the module level
615
 
<tt class="docutils literal"><span class="pre">dump()</span></tt> function that returns a recursive string representation for
 
615
<tt class="docutils literal">dump()</tt> function that returns a recursive string representation for
616
616
elements:</p>
617
617
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"""</span>
618
618
<span class="gp">... </span><span class="s">&lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;</span>
716
716
<p>Objectify uses two different types of Elements.  Structural Elements (or tree
717
717
Elements) represent the object tree structure.  Data Elements represent the
718
718
data containers at the leafs.  You can explicitly create tree Elements with
719
 
the <tt class="docutils literal"><span class="pre">objectify.Element()</span></tt> factory and data Elements with the
720
 
<tt class="docutils literal"><span class="pre">objectify.DataElement()</span></tt> factory.</p>
 
719
the <tt class="docutils literal">objectify.Element()</tt> factory and data Elements with the
 
720
<tt class="docutils literal">objectify.DataElement()</tt> factory.</p>
721
721
<p>When Element objects are created, lxml.objectify must determine which
722
722
implementation class to use for them.  This is relatively easy for tree
723
723
Elements and less so for data Elements.  The algorithm is as follows:</p>
734
734
<li>Otherwise, use the default class for empty data classes.</li>
735
735
</ol>
736
736
<p>You can change the default classes for tree Elements and empty data Elements
737
 
at setup time.  The <tt class="docutils literal"><span class="pre">ObjectifyElementClassLookup()</span></tt> call accepts two keyword
738
 
arguments, <tt class="docutils literal"><span class="pre">tree_class</span></tt> and <tt class="docutils literal"><span class="pre">empty_data_class</span></tt>, that determine the Element
739
 
classes used in these cases.  By default, <tt class="docutils literal"><span class="pre">tree_class</span></tt> is a class called
740
 
<tt class="docutils literal"><span class="pre">ObjectifiedElement</span></tt> and <tt class="docutils literal"><span class="pre">empty_data_class</span></tt> is a <tt class="docutils literal"><span class="pre">StringElement</span></tt>.</p>
 
737
at setup time.  The <tt class="docutils literal">ObjectifyElementClassLookup()</tt> call accepts two keyword
 
738
arguments, <tt class="docutils literal">tree_class</tt> and <tt class="docutils literal">empty_data_class</tt>, that determine the Element
 
739
classes used in these cases.  By default, <tt class="docutils literal">tree_class</tt> is a class called
 
740
<tt class="docutils literal">ObjectifiedElement</tt> and <tt class="docutils literal">empty_data_class</tt> is a <tt class="docutils literal">StringElement</tt>.</p>
741
741
<div class="section" id="type-annotations">
742
742
<h2>Type annotations</h2>
743
743
<p>The "type hint" mechanism deploys an XML attribute defined as
744
 
<tt class="docutils literal"><span class="pre">lxml.objectify.PYTYPE_ATTRIBUTE</span></tt>.  It may contain any of the following
 
744
<tt class="docutils literal">lxml.objectify.PYTYPE_ATTRIBUTE</tt>.  It may contain any of the following
745
745
string values: int, long, float, str, unicode, NoneType:</p>
746
746
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">objectify</span><span class="o">.</span><span class="n">PYTYPE_ATTRIBUTE</span><span class="p">)</span>
747
747
<span class="go">{http://codespeak.net/lxml/objectify/pytype}pytype</span>
763
763
<span class="go">None</span>
764
764
</pre></div>
765
765
<p>Note that you can change the name and namespace used for this
766
 
attribute through the <tt class="docutils literal"><span class="pre">set_pytype_attribute_tag(tag)</span></tt> module
 
766
attribute through the <tt class="docutils literal">set_pytype_attribute_tag(tag)</tt> module
767
767
function, in case your application ever needs to.  There is also a
768
 
utility function <tt class="docutils literal"><span class="pre">annotate()</span></tt> that recursively generates this
 
768
utility function <tt class="docutils literal">annotate()</tt> that recursively generates this
769
769
attribute for the elements of a tree:</p>
770
770
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"&lt;root&gt;&lt;a&gt;test&lt;/a&gt;&lt;b&gt;5&lt;/b&gt;&lt;/root&gt;"</span><span class="p">)</span>
771
771
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">objectify</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">root</span><span class="p">))</span>
805
805
<span class="go">    s = '5' [StringElement]</span>
806
806
<span class="go">      * xsi:type = 'xsd:string'</span>
807
807
</pre></div>
808
 
<p>Again, there is a utility function <tt class="docutils literal"><span class="pre">xsiannotate()</span></tt> that recursively
 
808
<p>Again, there is a utility function <tt class="docutils literal">xsiannotate()</tt> that recursively
809
809
generates the "xsi:type" attribute for the elements of a tree:</p>
810
810
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
811
811
<span class="gp">... </span><span class="s">   &lt;root&gt;&lt;a&gt;test&lt;/a&gt;&lt;b&gt;5&lt;/b&gt;&lt;c&gt;true&lt;/c&gt;&lt;/root&gt;</span>
827
827
<span class="go">    c = True [BoolElement]</span>
828
828
<span class="go">      * xsi:type = 'xsd:boolean'</span>
829
829
</pre></div>
830
 
<p>Note, however, that <tt class="docutils literal"><span class="pre">xsiannotate()</span></tt> will always use the first XML Schema
 
830
<p>Note, however, that <tt class="docutils literal">xsiannotate()</tt> will always use the first XML Schema
831
831
datatype that is defined for any given Python type, see also
832
832
<a class="reference internal" href="#defining-additional-data-classes">Defining additional data classes</a>.</p>
833
 
<p>The utility function <tt class="docutils literal"><span class="pre">deannotate()</span></tt> can be used to get rid of 'py:pytype'
 
833
<p>The utility function <tt class="docutils literal">deannotate()</tt> can be used to get rid of 'py:pytype'
834
834
and/or 'xsi:type' information:</p>
835
835
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
836
836
<span class="gp">... </span><span class="s">&lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span>
860
860
</pre></div>
861
861
<p>You can control which type attributes should be de-annotated with the keyword
862
862
arguments 'pytype' (default: True) and 'xsi' (default: True).
863
 
<tt class="docutils literal"><span class="pre">deannotate()</span></tt> can also remove 'xsi:nil' attributes by setting 'xsi_nil=True'
 
863
<tt class="docutils literal">deannotate()</tt> can also remove 'xsi:nil' attributes by setting 'xsi_nil=True'
864
864
(default: False):</p>
865
865
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">'''</span><span class="se">\</span>
866
866
<span class="gp">... </span><span class="s">&lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span>
896
896
</div>
897
897
<div class="section" id="the-dataelement-factory">
898
898
<h2>The DataElement factory</h2>
899
 
<p>For convenience, the <tt class="docutils literal"><span class="pre">DataElement()</span></tt> factory creates an Element with a
 
899
<p>For convenience, the <tt class="docutils literal">DataElement()</tt> factory creates an Element with a
900
900
Python value in one step.  You can pass the required Python type name or the
901
901
XSI type name:</p>
902
902
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s">"root"</span><span class="p">)</span>
920
920
<span class="go">      * py:pytype = 'int'</span>
921
921
<span class="go">      * xsi:type = 'xsd:integer'</span>
922
922
</pre></div>
923
 
<p>XML Schema types reside in the XML schema namespace thus <tt class="docutils literal"><span class="pre">DataElement()</span></tt>
 
923
<p>XML Schema types reside in the XML schema namespace thus <tt class="docutils literal">DataElement()</tt>
924
924
tries to correctly prefix the xsi:type attribute value for you:</p>
925
925
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s">"root"</span><span class="p">)</span>
926
926
<span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">s</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">DataElement</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">_xsi</span><span class="o">=</span><span class="s">"string"</span><span class="p">)</span>
931
931
<span class="go">  &lt;s xsi:type="xsd:string"&gt;5&lt;/s&gt;</span>
932
932
<span class="go">&lt;/root&gt;</span>
933
933
</pre></div>
934
 
<p><tt class="docutils literal"><span class="pre">DataElement()</span></tt> uses a default nsmap to set these prefixes:</p>
 
934
<p><tt class="docutils literal">DataElement()</tt> uses a default nsmap to set these prefixes:</p>
935
935
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">el</span> <span class="o">=</span> <span class="n">objectify</span><span class="o">.</span><span class="n">DataElement</span><span class="p">(</span><span class="s">'5'</span><span class="p">,</span> <span class="n">_xsi</span><span class="o">=</span><span class="s">'string'</span><span class="p">)</span>
936
936
<span class="gp">&gt;&gt;&gt; </span><span class="n">namespaces</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">el</span><span class="o">.</span><span class="n">nsmap</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
937
937
<span class="gp">&gt;&gt;&gt; </span><span class="n">namespaces</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
995
995
</pre></div>
996
996
<p>It is your responsibility to fix the prefixes of attribute values if you
997
997
choose to deviate from the standard prefixes.  A convenient way to do this for
998
 
xsi:type attributes is to use the <tt class="docutils literal"><span class="pre">xsiannotate()</span></tt> utility:</p>
 
998
xsi:type attributes is to use the <tt class="docutils literal">xsiannotate()</tt> utility:</p>
999
999
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">objectify</span><span class="o">.</span><span class="n">xsiannotate</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
1000
1000
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">etree</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">pretty_print</span><span class="o">=</span><span class="bp">True</span><span class="p">))</span>
1001
1001
<span class="go">&lt;root xmlns:schema="http://www.w3.org/2001/XMLSchema"&gt;</span>
1009
1009
<h2>Defining additional data classes</h2>
1010
1010
<p>You can plug additional data classes into objectify that will be used in
1011
1011
exactly the same way as the predefined types.  Data classes can either inherit
1012
 
from <tt class="docutils literal"><span class="pre">ObjectifiedDataElement</span></tt> directly or from one of the specialised
1013
 
classes like <tt class="docutils literal"><span class="pre">NumberElement</span></tt> or <tt class="docutils literal"><span class="pre">BoolElement</span></tt>.  The numeric types require
1014
 
an initial call to the NumberElement method <tt class="docutils literal"><span class="pre">self._setValueParser(function)</span></tt>
 
1012
from <tt class="docutils literal">ObjectifiedDataElement</tt> directly or from one of the specialised
 
1013
classes like <tt class="docutils literal">NumberElement</tt> or <tt class="docutils literal">BoolElement</tt>.  The numeric types require
 
1014
an initial call to the NumberElement method <tt class="docutils literal">self._setValueParser(function)</tt>
1015
1015
to set their type conversion function (string -&gt; numeric Python type).  This
1016
 
call should be placed into the element <tt class="docutils literal"><span class="pre">_init()</span></tt> method.</p>
1017
 
<p>The registration of data classes uses the <tt class="docutils literal"><span class="pre">PyType</span></tt> class:</p>
 
1016
call should be placed into the element <tt class="docutils literal">_init()</tt> method.</p>
 
1017
<p>The registration of data classes uses the <tt class="docutils literal">PyType</tt> class:</p>
1018
1018
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ChristmasDate</span><span class="p">(</span><span class="n">objectify</span><span class="o">.</span><span class="n">ObjectifiedDataElement</span><span class="p">):</span>
1019
1019
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">call_santa</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
1020
1020
<span class="gp">... </span>        <span class="k">print</span><span class="p">(</span><span class="s">"Ho ho ho!"</span><span class="p">)</span>
1029
1029
check and the custom data class.  If a type check is provided it must accept a
1030
1030
string as argument and raise ValueError or TypeError if it cannot handle the
1031
1031
string value.</p>
1032
 
<p>PyTypes are used if an element carries a <tt class="docutils literal"><span class="pre">py:pytype</span></tt> attribute denoting its
 
1032
<p>PyTypes are used if an element carries a <tt class="docutils literal">py:pytype</tt> attribute denoting its
1033
1033
data type or, in absence of such an attribute, if the given type check callable
1034
1034
does not raise a ValueError/TypeError exception when applied to the element
1035
1035
text.</p>
1036
1036
<p>If you want, you can also register this type under an XML Schema type name:</p>
1037
1037
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">xmas_type</span><span class="o">.</span><span class="n">xmlSchemaTypes</span> <span class="o">=</span> <span class="p">(</span><span class="s">"date"</span><span class="p">,)</span>
1038
1038
</pre></div>
1039
 
<p>XML Schema types will be considered if the element has an <tt class="docutils literal"><span class="pre">xsi:type</span></tt>
 
1039
<p>XML Schema types will be considered if the element has an <tt class="docutils literal">xsi:type</tt>
1040
1040
attribute that specifies its data type.  The line above binds the XSD type
1041
 
<tt class="docutils literal"><span class="pre">date</span></tt> to the newly defined Python type.  Note that this must be done before
 
1041
<tt class="docutils literal">date</tt> to the newly defined Python type.  Note that this must be done before
1042
1042
the next step, which is to register the type.  Then you can use it:</p>
1043
1043
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">xmas_type</span><span class="o">.</span><span class="n">register</span><span class="p">()</span>
1044
1044
 
1052
1052
<span class="nc">AttributeError: no such child</span>: <span class="n-Identifier">call_santa</span>
1053
1053
</pre></div>
1054
1054
<p>If you need to specify dependencies between the type check functions, you can
1055
 
pass a sequence of type names through the <tt class="docutils literal"><span class="pre">before</span></tt> and <tt class="docutils literal"><span class="pre">after</span></tt> keyword
1056
 
arguments of the <tt class="docutils literal"><span class="pre">register()</span></tt> method.  The PyType will then try to register
 
1055
pass a sequence of type names through the <tt class="docutils literal">before</tt> and <tt class="docutils literal">after</tt> keyword
 
1056
arguments of the <tt class="docutils literal">register()</tt> method.  The PyType will then try to register
1057
1057
itself before or after the respective types, as long as they are currently
1058
1058
registered.  Note that this only impacts the currently registered types at the
1059
1059
time of registration.  Types that are registered later on will not care about
1070
1070
<span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">call_santa</span><span class="p">()</span>
1071
1071
<span class="go">Ho ho ho!</span>
1072
1072
</pre></div>
1073
 
<p>To unregister a type, call its <tt class="docutils literal"><span class="pre">unregister()</span></tt> method:</p>
 
1073
<p>To unregister a type, call its <tt class="docutils literal">unregister()</tt> method:</p>
1074
1074
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">root</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">call_santa</span><span class="p">()</span>
1075
1075
<span class="go">Ho ho ho!</span>
1076
1076
<span class="gp">&gt;&gt;&gt; </span><span class="n">xmas_type</span><span class="o">.</span><span class="n">unregister</span><span class="p">()</span>
1086
1086
<div class="section" id="advanced-element-class-lookup">
1087
1087
<h2>Advanced element class lookup</h2>
1088
1088
<p>In some cases, the normal data class setup is not enough.  Being based
1089
 
on <tt class="docutils literal"><span class="pre">lxml.etree</span></tt>, however, <tt class="docutils literal"><span class="pre">lxml.objectify</span></tt> supports very
 
1089
on <tt class="docutils literal">lxml.etree</tt>, however, <tt class="docutils literal">lxml.objectify</tt> supports very
1090
1090
fine-grained control over the Element classes used in a tree.  All you
1091
1091
have to do is configure a different <a class="reference external" href="element_classes.html">class lookup</a> mechanism (or
1092
1092
write one yourself).</p>
1111
1111
support for <a class="reference external" href="element_classes.html#namespace-class-lookup">namespace specific classes</a>, you can register the
1112
1112
objectify lookup as a fallback of the namespace lookup.  In this case,
1113
1113
however, you have to take care that the namespace classes inherit from
1114
 
<tt class="docutils literal"><span class="pre">objectify.ObjectifiedElement</span></tt>, not only from the normal
1115
 
<tt class="docutils literal"><span class="pre">lxml.etree.ElementBase</span></tt>, so that they support the <tt class="docutils literal"><span class="pre">objectify</span></tt>
 
1114
<tt class="docutils literal">objectify.ObjectifiedElement</tt>, not only from the normal
 
1115
<tt class="docutils literal">lxml.etree.ElementBase</tt>, so that they support the <tt class="docutils literal">objectify</tt>
1116
1116
API.  The above setup code then becomes:</p>
1117
1117
<div class="syntax"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">lookup</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">ElementNamespaceClassLookup</span><span class="p">(</span>
1118
1118
<span class="gp">... </span>                  <span class="n">objectify</span><span class="o">.</span><span class="n">ObjectifyElementClassLookup</span><span class="p">()</span> <span class="p">)</span>
1128
1128
<ul class="simple">
1129
1129
<li>len(&lt;element&gt;) returns the sibling count, not the number of children of
1130
1130
&lt;element&gt;. You can retrieve the number of children with the
1131
 
<tt class="docutils literal"><span class="pre">countchildren()</span></tt> method.</li>
 
1131
<tt class="docutils literal">countchildren()</tt> method.</li>
1132
1132
<li>Iteration over elements does not yield the children, but the siblings.  You
1133
 
can access all children with the <tt class="docutils literal"><span class="pre">iterchildren()</span></tt> method on elements or
1134
 
retrieve a list by calling the <tt class="docutils literal"><span class="pre">getchildren()</span></tt> method.</li>
 
1133
can access all children with the <tt class="docutils literal">iterchildren()</tt> method on elements or
 
1134
retrieve a list by calling the <tt class="docutils literal">getchildren()</tt> method.</li>
1135
1135
<li>The find, findall and findtext methods require a different implementation
1136
 
based on ETXPath.  In <tt class="docutils literal"><span class="pre">lxml.etree</span></tt>, they use a Python implementation based
 
1136
based on ETXPath.  In <tt class="docutils literal">lxml.etree</tt>, they use a Python implementation based
1137
1137
on the original iteration scheme.  This has the disadvantage that they may
1138
1138
not be 100% backwards compatible, and the additional advantage that they now
1139
1139
support any XPath expression.</li>
1142
1142
</div>
1143
1143
<div class="footer">
1144
1144
<hr class="footer" />
1145
 
Generated on: 2009-11-11.
 
1145
Generated on: 2010-03-02.
1146
1146
 
1147
1147
</div>
1148
1148
</body>