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 streamPython file objectThe stream to be read for XML text ownerDocxml.dom.Document.DocumentA 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 xmlStringstring or unicode objectThe string to be parsed for XML text ownerDocxml.dom.Document.DocumentA 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 uriPython file objectThe uri from which XML text is to be retrieved ownerDocxml.dom.Document.DocumentA 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 streamPython file objectThe stream to be read for HTML text ownerDocxml.dom.Document.DocumentA 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. charsetstringThe 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 htmlStringstring or unicode objectThe string to be parsed for HTML text ownerDocxml.dom.Document.DocumentA 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. charsetstringThe 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 uriPython file objectThe uri from which HTML text is to be retrieved ownerDocxml.dom.Document.DocumentA 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. charsetstringThe 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. rootxml.dom.NodeThe node to be printed, with all its children recursively. streamoutput streamThe output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout. encodingstringThe 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. rootxml.dom.NodeThe node to be pretty-printed, with all its children recursively. streamoutput streamThe output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout. encodingstringThe character encoding to use for output. Default is 'UTF-8'. indentstringThe amount by which nested constructs are indented when printed on a fresh line. Default is '\t'. widthpositive integerThe width of the output console. Used to make line-break decisions. Default is 80. preserveElementslist 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. rootxml.dom.NodeThe HTML node to be printed, with all its children recursively. streamoutput streamThe output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout. encodingstringThe 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. rootxml.dom.NodeThe node to be pretty-printed, with all its children recursively. streamoutput streamThe output stream. Note: can be a StringIO object if you want to generate a string instead. Default is sys.stdout. encodingstringThe character encoding to use for output. Default is 'UTF-8'. indentstringThe amount by which nested constructs are indented when printed on a fresh line. Default is '\t'. widthpositive integerThe width of the output console. Used to make line-break decisions. Default is 80. preserveElementslist 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. nodeTypeOne of the integers defined as node types in xml.dom.NodeThe node type to look up. stringName of corresponding DOM interface from spec. xml.dom.ext.StripHtml Strips extraneous white-space from an HTML DOM tree. startNodexml.dom.NodeThe node to be stripped, with all its children recursively. preserveElementslist 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.NodeThe 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. startNodexml.dom.NodeThe node to be stripped, with all its children recursively. preserveElementslist 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.NodeThe startNode with descendant ignorable white-space stripped. xml.dom.ext.GetElementById Returns the element node whose "ID" attribute is as given. startNodexml.dom.NodeThe node whose descendants are to be searched. targetIdstring conforming to XML ID typeThe XML ID to find. xml.dom.ElementThe 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. nodexml.dom.NodeThe node for which all in-scope namespaces are returned. doctionaryDictionary 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). nodexml.dom.NodeThe 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. qnamestring matching QName production in XML Namespaces 1.0 specThe 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.