4DOM Extensions

# 4DOM Extensions

4DOM
Extensions
DOM
Documentation on 4DOM extensions and deviations from the DOM specification.

These are utility classes and functions that provide capabilities not yet specified in the DOM spec. Some of these facilities, such as factories and readers are expected to be specified in later levels of the DOM, so we try to keep our proprietary interfaces simple for now so that you can more painlessly migrate when relevant standards emerge.

See the demo directory for examples exercising many of these extensions.

# Reading

The Reader package allows you to parse source strings in XML and HTML into DOM trees. You select a reader module according to the nature of your input. The readers that come with 4DOM are as follows:

PyExpat
  Read XML using pyexpat from PyXML. Does not support validation.
HtmlLib
  Read HTML using Python's htmllib.
Sax2
  Read XML using the PyXML SAX2 package. DTD validation is option.
Sax (deprecated)
  Read XML using the PyXML SAX package. DTD validation is option.
Sgmlop
  Read XML using Sgmlop from PyXML. Does not support validation.

The following two examples illustrate using PyExpat and HtmlLib readers. Replace with the appropriate module and use in your own code.

    # Parse XML using pyexpat
    from xml.dom.ext.reader import PyExpat
    reader = PyExpat.Reader()
    xml_doc = reader.fromStream(stream)

    #Parse HTML using htmllib
    from xml.dom.ext.reader import HtmlLib
    reader = HtmlLib.Reader()
    html_doc = reader.fromStream(stream)

PyExpat
Parse XML using pyexpat
Reader
Reusable utility to read XML documents.
fromStream
return a 4DOM node from the given stream
stream
Python file object The stream to be read for XML text
ownerDoc
xml.dom.Document.Document A document to be used as owner of all the created nodes. If None, a new document instance is created for the nodes. Default is None.
xml.dom.Document.Document or xml.dom.DocumentFragment.DocumentFragmenta new document instance, or if the ownerDoc argument was not None, a document fragment. In either case, the returned node roots the created XML tree.
fromString
return a 4DOM node from the given string
xmlString
string or unicode object The string to be parsed for XML text
ownerDoc
xml.dom.Document.Document A document to be used as owner of all the created nodes. If None, a new document instance is created for the nodes. Default is None.
xml.dom.Document.Document or xml.dom.DocumentFragment.DocumentFragmenta new document instance, or if the ownerDoc argument was not None, a document fragment. In either case, the returned node roots the created XML tree.
fromUri
return a 4DOM node from the given uri
uri
Python file object The uri from which XML text is to be retrieved
ownerDoc
xml.dom.Document.Document A document to be used as owner of all the created nodes. If None, a new document instance is created for the nodes. Default is None.
xml.dom.Document.Document or xml.dom.DocumentFragment.DocumentFragmenta new document instance, or if the ownerDoc argument was not None, a document fragment. In either case, the returned node roots the created XML tree.
HtmlLib
Parse HTML using htmllib
Reader
Reusable utility to read HTML documents.
fromStream
return a 4DOM node from the given stream
stream
Python file object The stream to be read for HTML text
ownerDoc
xml.dom.Document.Document A document to be used as owner of all the created nodes. If None, a new document instance is created for the nodes. Default is None.
charset
string The character set of the HTML text. If None or empty string, the default is ISO-8859-1. Default is empty string.
xml.dom.Document.Document or xml.dom.DocumentFragment.DocumentFragmenta new document instance, or if the ownerDoc argument was not None, a document fragment. In either case, the returned node roots the created HTML tree.
fromString
return a 4DOM node from the given string
htmlString
string or unicode object The string to be parsed for HTML text
ownerDoc
xml.dom.Document.Document A document to be used as owner of all the created nodes. If None, a new document instance is created for the nodes. Default is None.
charset
string The character set of the HTML text. If None or empty string, the default is ISO-8859-1. Default is empty string.
xml.dom.Document.Document or xml.dom.DocumentFragment.DocumentFragmenta new document instance, or if the ownerDoc argument was not None, a document fragment. In either case, the returned node roots the created HTML tree.
fromUri
return a 4DOM node from the given uri
uri
Python file object The uri from which HTML text is to be retrieved
ownerDoc
xml.dom.Document.Document A document to be used as owner of all the created nodes. If None, a new document instance is created for the nodes. Default is None.
charset
string The character set of the HTML text. If None or empty string, the default is ISO-8859-1. Default is empty string.
xml.dom.Document.Document or xml.dom.DocumentFragment.DocumentFragmenta new document instance, or if the ownerDoc argument was not None, a document fragment. In either case, the returned node roots the created HTML tree.

# Printing/Writing

The Printer module allows you to write a text representation of DOM nodes to an output stream, including stdout. Note that limitations in the SAX interface used to parse in XML files, and in the DOM spec itself make it impossible at this point to handle an unchanged "round trip". That is, if you use the builder to build a DOM node from text and then use the Printer to turn it back to text, there may be differences; some may be significant.

The easiest way to use the Printer module is through the front-end functions in the xml.dom.ext package.

xml.dom.ext.Print
Render the DOM tree to text with no special formatting.
root
xml.dom.Node The node to be printed, with all its children recursively.
stream
output stream The output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout.
encoding
string The character encoding to use for output. Default is 'UTF-8'.
xml.dom.ext.PrettyPrint
Render the DOM tree to text, with added indentation and new-lines for enhanced readability.
root
xml.dom.Node The node to be pretty-printed, with all its children recursively.
stream
output stream The output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout.
encoding
string The character encoding to use for output. Default is 'UTF-8'.
indent
string The amount by which nested constructs are indented when printed on a fresh line. Default is '\t'.
width
positive integer The width of the output console. Used to make line-break decisions. Default is 80.
preserveElements
list of strings, each of which is an SGML generic identifier. Specifes elements in which white-space shouldn't be added. Note that white-space is never added to in-line elements in an HTMLDocument. Default is None.
xml.dom.ext.XHtmlPrint
Render an HTML DOM tree as XHTML with no special indentation or formatting.
root
xml.dom.Node The HTML node to be printed, with all its children recursively.
stream
output stream The output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout.
encoding
string The character encoding to use for output. Default is 'UTF-8'.
xml.dom.ext.XHtmlPrettyPrint
Render an HTML DOM tree to text, with added indentation and new-lines for enhanced readability.
root
xml.dom.Node The node to be pretty-printed, with all its children recursively.
stream
output stream The output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout.
encoding
string The character encoding to use for output. Default is 'UTF-8'.
indent
string The amount by which nested constructs are indented when printed on a fresh line. Default is '\t'.
width
positive integer The width of the output console. Used to make line-break decisions. Default is 80.
preserveElements
list of strings, each of which is an SGML generic identifier. Specifes elements in which white-space shouldn't be added. Note that white-space is never added to in-line elements in an HTMLDocument. Default is None.

# Miscellaneous

xml.dom.ext.NodeTypeToInterface
Look up a node type (as returned from getNodeType()) and returns a corresponding interface name.
nodeType
One of the integers defined as node types in xml.dom.Node The node type to look up.
string Name of corresponding DOM interface from spec.
xml.dom.ext.StripHtml
Strips extraneous white-space from an HTML DOM tree.
startNode
xml.dom.Node The node to be stripped, with all its children recursively.
preserveElements
list of strings, each of which is an SGML generic identifier, or None to indicate an empty list. Specifes elements from which white-space shouldn't be stripped. Note that white-space is never stripped from in-line elements in an HTMLDocument. Default is None.
xml.dom.Node The startNode with descendant ignorable white-space stripped.
xml.dom.ext.StripXml
Strips extraneous white-space from an XML DOM tree. Takes xml:space attributes into account.
startNode
xml.dom.Node The node to be stripped, with all its children recursively.
preserveElements
list of strings, each of which is an SGML generic identifier, or None to indicate an empty list. Specifes elements from which white-space shouldn't be stripped. Default is None.
xml.dom.Node The startNode with descendant ignorable white-space stripped.
xml.dom.ext.GetElementById
Returns the element node whose "ID" attribute is as given.
startNode
xml.dom.Node The node whose descendants are to be searched.
targetId
string conforming to XML ID type The XML ID to find.
xml.dom.Element The elemtn with the given ID, or None to indicate no match.
xml.dom.ext.GetAllNs
Returns all the namespaces in effect on the given node, including the default namespace and the xml namespace.
node
xml.dom.Node The node for which all in-scope namespaces are returned.
doctionary Dictionary mapping all in-scope namespaces to URIs, with '' as prefix for the default namespace.
xml.dom.ext.XmlSpaceState
Determines whether the xml:space state at a given node is "preserve" or "default" (See the XML 1.0 spec).
node
xml.dom.Node The node whose space state is to be found.
string "preserve" or "default".
xml.dom.ext.SplitQName
Splits a valid QName from the XML Namespaces 1.0 spec into prefix and suffix (the local name in the case of element and attribute names, and the declared prefix in the case of namespace declarations.
qname
string matching QName production in XML Namespaces 1.0 spec The name to be split.
tuple with 2 items. a tuple of the form (prefix, suffix). If there is exactly one colon in the qname, prefix is the part before and suffix the part after the colon. Otherwise prefix is '' and suffix is the entire input string.