paulb@18 | 1 | #!/usr/bin/env python |
paulb@18 | 2 | |
paulb@18 | 3 | """ |
paulb@75 | 4 | DOM wrapper around libxml2, specifically the libxml2mod Python extension module. |
paulb@18 | 5 | """ |
paulb@18 | 6 | |
paulb@131 | 7 | __version__ = "0.2.2" |
paulb@27 | 8 | |
paulb@18 | 9 | import libxml2 |
paulb@72 | 10 | from libxml2dom.macrolib import * |
paulb@72 | 11 | from libxml2dom.macrolib import \ |
paulb@72 | 12 | createDocument as Node_createDocument, \ |
paulb@72 | 13 | parseString as Node_parseString, parseURI as Node_parseURI, \ |
paulb@72 | 14 | parseFile as Node_parseFile, \ |
paulb@72 | 15 | toString as Node_toString, toStream as Node_toStream, \ |
paulb@72 | 16 | toFile as Node_toFile |
paulb@75 | 17 | import weakref |
paulb@18 | 18 | |
paulb@86 | 19 | # Attribute and node list wrappers. |
paulb@18 | 20 | |
paulb@18 | 21 | class NamedNodeMap(object): |
paulb@18 | 22 | |
paulb@75 | 23 | """ |
paulb@75 | 24 | A wrapper around Node objects providing DOM and dictionary convenience |
paulb@75 | 25 | methods. |
paulb@75 | 26 | """ |
paulb@75 | 27 | |
paulb@18 | 28 | def __init__(self, node): |
paulb@18 | 29 | self.node = node |
paulb@18 | 30 | |
paulb@18 | 31 | def getNamedItem(self, name): |
paulb@72 | 32 | return self.node.getAttributeNode(name) |
paulb@18 | 33 | |
paulb@18 | 34 | def getNamedItemNS(self, ns, localName): |
paulb@72 | 35 | return self.node.getAttributeNodeNS(ns, localName) |
paulb@18 | 36 | |
paulb@18 | 37 | def setNamedItem(self, node): |
paulb@91 | 38 | try: |
paulb@91 | 39 | old = self.getNamedItem(node.nodeName) |
paulb@91 | 40 | except KeyError: |
paulb@91 | 41 | old = None |
paulb@86 | 42 | self.node.setAttributeNode(node) |
paulb@91 | 43 | return old |
paulb@18 | 44 | |
paulb@18 | 45 | def setNamedItemNS(self, node): |
paulb@91 | 46 | try: |
paulb@91 | 47 | old = self.getNamedItemNS(node.namespaceURI, node.localName) |
paulb@91 | 48 | except KeyError: |
paulb@91 | 49 | old = None |
paulb@86 | 50 | self.node.setAttributeNodeNS(node) |
paulb@91 | 51 | return old |
paulb@91 | 52 | |
paulb@91 | 53 | def removeNamedItem(self, name): |
paulb@91 | 54 | try: |
paulb@91 | 55 | old = self.getNamedItem(name) |
paulb@91 | 56 | except KeyError: |
paulb@91 | 57 | old = None |
paulb@91 | 58 | self.node.removeAttribute(name) |
paulb@91 | 59 | return old |
paulb@91 | 60 | |
paulb@91 | 61 | def removeNamedItemNS(self, ns, localName): |
paulb@91 | 62 | try: |
paulb@91 | 63 | old = self.getNamedItemNS(ns, localName) |
paulb@91 | 64 | except KeyError: |
paulb@91 | 65 | old = None |
paulb@91 | 66 | self.node.removeAttributeNS(ns, localName) |
paulb@91 | 67 | return old |
paulb@86 | 68 | |
paulb@86 | 69 | # Dictionary emulation methods. |
paulb@18 | 70 | |
paulb@18 | 71 | def __getitem__(self, name): |
paulb@18 | 72 | return self.getNamedItem(name) |
paulb@18 | 73 | |
paulb@18 | 74 | def __setitem__(self, name, node): |
paulb@18 | 75 | if name == node.nodeName: |
paulb@18 | 76 | self.setNamedItem(node) |
paulb@18 | 77 | else: |
paulb@18 | 78 | raise KeyError, name |
paulb@18 | 79 | |
paulb@18 | 80 | def __delitem__(self, name): |
paulb@18 | 81 | # NOTE: To be implemented. |
paulb@18 | 82 | pass |
paulb@18 | 83 | |
paulb@18 | 84 | def values(self): |
paulb@88 | 85 | return [Attribute(_node, self.node.ownerDocument) for _node in Node_attributes(self.node.as_native_node()).values()] |
paulb@18 | 86 | |
paulb@18 | 87 | def keys(self): |
paulb@18 | 88 | return [(attr.namespaceURI, attr.localName) for attr in self.values()] |
paulb@18 | 89 | |
paulb@18 | 90 | def items(self): |
paulb@18 | 91 | return [((attr.namespaceURI, attr.localName), attr) for attr in self.values()] |
paulb@18 | 92 | |
paulb@18 | 93 | def __repr__(self): |
paulb@18 | 94 | return str(self) |
paulb@18 | 95 | |
paulb@18 | 96 | def __str__(self): |
paulb@18 | 97 | return "{%s}" % ",\n".join(["%s : %s" % (repr(key), repr(value)) for key, value in self.items()]) |
paulb@18 | 98 | |
paulb@86 | 99 | class NodeList(list): |
paulb@86 | 100 | |
paulb@86 | 101 | "A wrapper around node lists." |
paulb@86 | 102 | |
paulb@86 | 103 | def item(self, index): |
paulb@86 | 104 | return self[index] |
paulb@86 | 105 | |
paulb@86 | 106 | def length(self): |
paulb@86 | 107 | return len(self) |
paulb@86 | 108 | |
paulb@86 | 109 | # Node classes. |
paulb@86 | 110 | |
paulb@18 | 111 | class Node(object): |
paulb@18 | 112 | |
paulb@75 | 113 | """ |
paulb@75 | 114 | A DOM-style wrapper around libxml2mod objects. |
paulb@75 | 115 | """ |
paulb@75 | 116 | |
paulb@81 | 117 | ATTRIBUTE_NODE = xml.dom.Node.ATTRIBUTE_NODE |
paulb@81 | 118 | COMMENT_NODE = xml.dom.Node.COMMENT_NODE |
paulb@81 | 119 | DOCUMENT_NODE = xml.dom.Node.DOCUMENT_NODE |
paulb@81 | 120 | DOCUMENT_TYPE_NODE = xml.dom.Node.DOCUMENT_TYPE_NODE |
paulb@81 | 121 | ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE |
paulb@81 | 122 | ENTITY_NODE = xml.dom.Node.ENTITY_NODE |
paulb@81 | 123 | ENTITY_REFERENCE_NODE = xml.dom.Node.ENTITY_REFERENCE_NODE |
paulb@81 | 124 | NOTATION_NODE = xml.dom.Node.NOTATION_NODE |
paulb@81 | 125 | PROCESSING_INSTRUCTION_NODE = xml.dom.Node.PROCESSING_INSTRUCTION_NODE |
paulb@81 | 126 | TEXT_NODE = xml.dom.Node.TEXT_NODE |
paulb@81 | 127 | |
paulb@75 | 128 | def __init__(self, node, ownerDocument=None): |
paulb@18 | 129 | self._node = node |
paulb@75 | 130 | self.ownerDocument = ownerDocument |
paulb@18 | 131 | |
paulb@18 | 132 | def as_native_node(self): |
paulb@18 | 133 | return self._node |
paulb@18 | 134 | |
paulb@18 | 135 | def _nodeType(self): |
paulb@72 | 136 | return Node_nodeType(self._node) |
paulb@18 | 137 | |
paulb@18 | 138 | def _childNodes(self): |
paulb@18 | 139 | |
paulb@18 | 140 | # NOTE: Consider a generator instead. |
paulb@18 | 141 | |
paulb@86 | 142 | return NodeList([Node(_node, self.ownerDocument) for _node in Node_childNodes(self._node)]) |
paulb@18 | 143 | |
paulb@18 | 144 | def _attributes(self): |
paulb@18 | 145 | return NamedNodeMap(self) |
paulb@18 | 146 | |
paulb@18 | 147 | def _namespaceURI(self): |
paulb@72 | 148 | return Node_namespaceURI(self._node) |
paulb@18 | 149 | |
paulb@18 | 150 | def _nodeValue(self): |
paulb@72 | 151 | return Node_nodeValue(self._node) |
paulb@18 | 152 | |
paulb@86 | 153 | def _setNodeValue(self, value): |
paulb@86 | 154 | Node_setNodeValue(self._node, value) |
paulb@86 | 155 | |
paulb@18 | 156 | def _prefix(self): |
paulb@72 | 157 | return Node_prefix(self._node) |
paulb@18 | 158 | |
paulb@18 | 159 | def _nodeName(self): |
paulb@72 | 160 | return Node_nodeName(self._node) |
paulb@18 | 161 | |
paulb@18 | 162 | def _tagName(self): |
paulb@72 | 163 | return Node_tagName(self._node) |
paulb@18 | 164 | |
paulb@18 | 165 | def _localName(self): |
paulb@72 | 166 | return Node_localName(self._node) |
paulb@18 | 167 | |
paulb@18 | 168 | def _parentNode(self): |
paulb@88 | 169 | return get_node(Node_parentNode(self._node), self) |
paulb@18 | 170 | |
paulb@24 | 171 | def _previousSibling(self): |
paulb@75 | 172 | return Node(Node_previousSibling(self._node), self.ownerDocument) |
paulb@24 | 173 | |
paulb@18 | 174 | def _nextSibling(self): |
paulb@75 | 175 | return Node(Node_nextSibling(self._node), self.ownerDocument) |
paulb@18 | 176 | |
paulb@18 | 177 | def hasAttributeNS(self, ns, localName): |
paulb@72 | 178 | return Node_hasAttributeNS(self._node, ns, localName) |
paulb@18 | 179 | |
paulb@18 | 180 | def hasAttribute(self, name): |
paulb@72 | 181 | return Node_hasAttribute(self._node, name) |
paulb@18 | 182 | |
paulb@18 | 183 | def getAttributeNS(self, ns, localName): |
paulb@72 | 184 | return Node_getAttributeNS(self._node, ns, localName) |
paulb@18 | 185 | |
paulb@18 | 186 | def getAttribute(self, name): |
paulb@72 | 187 | return Node_getAttribute(self._node, name) |
paulb@18 | 188 | |
paulb@18 | 189 | def getAttributeNodeNS(self, ns, localName): |
paulb@88 | 190 | return Attribute(Node_getAttributeNodeNS(self._node, ns, localName), self.ownerDocument) |
paulb@18 | 191 | |
paulb@18 | 192 | def getAttributeNode(self, localName): |
paulb@88 | 193 | return Attribute(Node_getAttributeNode(self._node, localName), self.ownerDocument) |
paulb@18 | 194 | |
paulb@18 | 195 | def setAttributeNS(self, ns, name, value): |
paulb@72 | 196 | Node_setAttributeNS(self._node, ns, name, value) |
paulb@18 | 197 | |
paulb@18 | 198 | def setAttribute(self, name, value): |
paulb@72 | 199 | Node_setAttribute(self._node, name, value) |
paulb@18 | 200 | |
paulb@86 | 201 | def setAttributeNodeNS(self, node): |
paulb@88 | 202 | Node_setAttributeNodeNS(self._node, node._node) |
paulb@18 | 203 | |
paulb@86 | 204 | def setAttributeNode(self, node): |
paulb@88 | 205 | Node_setAttributeNode(self._node, node._node) |
paulb@18 | 206 | |
paulb@91 | 207 | def removeAttributeNS(self, ns, localName): |
paulb@91 | 208 | Node_removeAttributeNS(self._node, ns, localName) |
paulb@91 | 209 | |
paulb@91 | 210 | def removeAttribute(self, name): |
paulb@91 | 211 | Node_removeAttribute(self._node, name) |
paulb@91 | 212 | |
paulb@18 | 213 | def createElementNS(self, ns, name): |
paulb@75 | 214 | return Node(Node_createElementNS(self._node, ns, name), self.ownerDocument) |
paulb@18 | 215 | |
paulb@18 | 216 | def createElement(self, name): |
paulb@75 | 217 | return Node(Node_createElement(self._node, name), self.ownerDocument) |
paulb@18 | 218 | |
paulb@88 | 219 | def createAttributeNS(self, ns, name): |
paulb@88 | 220 | tmp = self.createElement("tmp") |
paulb@88 | 221 | return Attribute(Node_createAttributeNS(tmp._node, ns, name)) |
paulb@18 | 222 | |
paulb@88 | 223 | def createAttribute(self, name): |
paulb@88 | 224 | tmp = self.createElement("tmp") |
paulb@88 | 225 | return Attribute(Node_createAttribute(tmp._node, name)) |
paulb@18 | 226 | |
paulb@18 | 227 | def createTextNode(self, value): |
paulb@75 | 228 | return Node(Node_createTextNode(self._node, value), self.ownerDocument) |
paulb@18 | 229 | |
paulb@34 | 230 | def createComment(self, value): |
paulb@75 | 231 | return Node(Node_createComment(self._node, value), self.ownerDocument) |
paulb@18 | 232 | |
paulb@18 | 233 | def importNode(self, node, deep): |
paulb@72 | 234 | if hasattr(node, "as_native_node"): |
paulb@75 | 235 | return Node(Node_importNode(self._node, node.as_native_node(), deep), self.ownerDocument) |
paulb@72 | 236 | else: |
paulb@75 | 237 | return Node(Node_importNode_DOM(self._node, node, deep), self.ownerDocument) |
paulb@18 | 238 | |
paulb@18 | 239 | def insertBefore(self, tmp, oldNode): |
paulb@72 | 240 | if hasattr(tmp, "as_native_node"): |
paulb@75 | 241 | return Node(Node_insertBefore(self._node, tmp.as_native_node(), oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 242 | else: |
paulb@75 | 243 | return Node(Node_insertBefore(self._node, tmp, oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 244 | |
paulb@18 | 245 | def replaceChild(self, tmp, oldNode): |
paulb@72 | 246 | if hasattr(tmp, "as_native_node"): |
paulb@75 | 247 | return Node(Node_replaceChild(self._node, tmp.as_native_node(), oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 248 | else: |
paulb@75 | 249 | return Node(Node_replaceChild(self._node, tmp, oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 250 | |
paulb@18 | 251 | def appendChild(self, tmp): |
paulb@72 | 252 | if hasattr(tmp, "as_native_node"): |
paulb@75 | 253 | return Node(Node_appendChild(self._node, tmp.as_native_node()), self.ownerDocument) |
paulb@18 | 254 | else: |
paulb@75 | 255 | return Node(Node_appendChild(self._node, tmp), self.ownerDocument) |
paulb@18 | 256 | |
paulb@18 | 257 | def removeChild(self, tmp): |
paulb@72 | 258 | if hasattr(tmp, "as_native_node"): |
paulb@72 | 259 | Node_removeChild(self._node, tmp.as_native_node()) |
paulb@72 | 260 | else: |
paulb@72 | 261 | Node_removeChild(self._node, tmp) |
paulb@18 | 262 | |
paulb@99 | 263 | def getElementsByTagName(self, tagName): |
paulb@99 | 264 | return self.xpath("//" + tagName) |
paulb@99 | 265 | |
paulb@99 | 266 | def getElementsByTagNameNS(self, namespaceURI, localName): |
paulb@99 | 267 | return self.xpath("//ns:" + localName, namespaces={"ns" : namespaceURI}) |
paulb@99 | 268 | |
paulb@124 | 269 | def normalize(self): |
paulb@124 | 270 | text_nodes = [] |
paulb@124 | 271 | for node in self.childNodes: |
paulb@124 | 272 | if node.nodeType == node.TEXT_NODE: |
paulb@124 | 273 | text_nodes.append(node) |
paulb@124 | 274 | elif len(text_nodes) != 0: |
paulb@124 | 275 | self._normalize(text_nodes) |
paulb@124 | 276 | text_nodes = [] |
paulb@124 | 277 | if len(text_nodes) != 0: |
paulb@124 | 278 | self._normalize(text_nodes) |
paulb@124 | 279 | |
paulb@124 | 280 | def _normalize(self, text_nodes): |
paulb@124 | 281 | texts = [] |
paulb@124 | 282 | for text_node in text_nodes[:-1]: |
paulb@124 | 283 | texts.append(text_node.nodeValue) |
paulb@124 | 284 | self.removeChild(text_node) |
paulb@124 | 285 | texts.append(text_nodes[-1].nodeValue) |
paulb@124 | 286 | self.replaceChild(self.ownerDocument.createTextNode("".join(texts)), text_nodes[-1]) |
paulb@124 | 287 | |
paulb@99 | 288 | # NOTE: normalize must be implemented specially for libxml2dom. |
paulb@99 | 289 | |
paulb@18 | 290 | childNodes = property(_childNodes) |
paulb@86 | 291 | value = data = nodeValue = property(_nodeValue, _setNodeValue) |
paulb@18 | 292 | name = nodeName = property(_nodeName) |
paulb@18 | 293 | tagName = property(_tagName) |
paulb@18 | 294 | namespaceURI = property(_namespaceURI) |
paulb@18 | 295 | prefix = property(_prefix) |
paulb@18 | 296 | localName = property(_localName) |
paulb@88 | 297 | parentNode = property(_parentNode) |
paulb@18 | 298 | nodeType = property(_nodeType) |
paulb@18 | 299 | attributes = property(_attributes) |
paulb@24 | 300 | previousSibling = property(_previousSibling) |
paulb@18 | 301 | nextSibling = property(_nextSibling) |
paulb@18 | 302 | |
paulb@72 | 303 | #def isSameNode(self, other): |
paulb@72 | 304 | # return self._node.nodePath() == other._node.nodePath() |
paulb@18 | 305 | |
paulb@72 | 306 | #def __eq__(self, other): |
paulb@72 | 307 | # return self._node.nodePath() == other._node.nodePath() |
paulb@18 | 308 | |
paulb@32 | 309 | # 4DOM extensions to the usual PyXML API. |
paulb@32 | 310 | # NOTE: To be finished. |
paulb@32 | 311 | |
paulb@32 | 312 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@81 | 313 | result = Node_xpath(self._node, expr, variables, namespaces) |
paulb@83 | 314 | if hasattr(result, "__len__"): |
paulb@88 | 315 | return NodeList([get_node(_node, self) for _node in result]) |
paulb@81 | 316 | else: |
paulb@81 | 317 | return result |
paulb@81 | 318 | |
paulb@81 | 319 | # Convenience methods. |
paulb@81 | 320 | |
paulb@120 | 321 | def toString(self, encoding=None, prettyprint=0): |
paulb@120 | 322 | return toString(self, encoding, prettyprint) |
paulb@81 | 323 | |
paulb@120 | 324 | def toStream(self, stream, encoding=None, prettyprint=0): |
paulb@120 | 325 | toStream(self, stream, encoding, prettyprint) |
paulb@81 | 326 | |
paulb@120 | 327 | def toFile(self, f, encoding=None, prettyprint=0): |
paulb@120 | 328 | toFile(self, f, encoding, prettyprint) |
paulb@75 | 329 | |
paulb@88 | 330 | # Attribute nodes. |
paulb@88 | 331 | |
paulb@88 | 332 | class Attribute(Node): |
paulb@88 | 333 | |
paulb@88 | 334 | "A class providing attribute access." |
paulb@88 | 335 | |
paulb@88 | 336 | def __init__(self, node, ownerDocument=None, ownerElement=None): |
paulb@88 | 337 | Node.__init__(self, node, ownerDocument) |
paulb@88 | 338 | self.ownerElement = ownerElement |
paulb@88 | 339 | |
paulb@88 | 340 | def _parentNode(self): |
paulb@88 | 341 | return self.ownerElement |
paulb@88 | 342 | |
paulb@88 | 343 | parentNode = property(_parentNode) |
paulb@88 | 344 | |
paulb@75 | 345 | # Document housekeeping mechanisms. |
paulb@75 | 346 | |
paulb@75 | 347 | class Document(Node): |
paulb@75 | 348 | |
paulb@75 | 349 | "A class providing document-level housekeeping." |
paulb@75 | 350 | |
paulb@75 | 351 | def __init__(self, node): |
paulb@75 | 352 | self._node = node |
paulb@75 | 353 | self.weakref_ownerDocument = weakref.ref(self) |
paulb@75 | 354 | |
paulb@75 | 355 | def _ownerDocument(self): |
paulb@75 | 356 | return self.weakref_ownerDocument() |
paulb@75 | 357 | |
paulb@75 | 358 | def __del__(self): |
paulb@84 | 359 | #print "Freeing document", self._node |
paulb@75 | 360 | libxml2mod.xmlFreeDoc(self._node) |
paulb@75 | 361 | |
paulb@75 | 362 | ownerDocument = property(_ownerDocument) |
paulb@32 | 363 | |
paulb@88 | 364 | # Factory functions. |
paulb@88 | 365 | |
paulb@88 | 366 | def get_node(_node, context_node): |
paulb@88 | 367 | if Node_nodeType(_node) == context_node.DOCUMENT_NODE: |
paulb@88 | 368 | return context_node.ownerDocument |
paulb@88 | 369 | elif Node_nodeType(_node) == context_node.ATTRIBUTE_NODE: |
paulb@88 | 370 | return Attribute(_node, context_node.ownerDocument, context_node) |
paulb@88 | 371 | else: |
paulb@88 | 372 | return Node(_node, context_node.ownerDocument) |
paulb@88 | 373 | |
paulb@18 | 374 | # Utility functions. |
paulb@18 | 375 | |
paulb@18 | 376 | def createDocumentType(localName, publicId, systemId): |
paulb@18 | 377 | return None |
paulb@18 | 378 | |
paulb@18 | 379 | def createDocument(namespaceURI, localName, doctype): |
paulb@75 | 380 | return Document(Node_createDocument(namespaceURI, localName, doctype)) |
paulb@18 | 381 | |
paulb@103 | 382 | def parse(stream_or_string, html=0): |
paulb@105 | 383 | |
paulb@105 | 384 | """ |
paulb@105 | 385 | Parse the given 'stream_or_string', where the supplied object can either be |
paulb@105 | 386 | a stream (such as a file or stream object), or a string (containing the text |
paulb@105 | 387 | of a document). If the optional 'html' parameter is set to a true value, the |
paulb@105 | 388 | content to be parsed will be treated as being HTML rather than XML. |
paulb@105 | 389 | |
paulb@105 | 390 | A document object is returned by this function. |
paulb@105 | 391 | """ |
paulb@105 | 392 | |
paulb@18 | 393 | if hasattr(stream_or_string, "read"): |
paulb@18 | 394 | stream = stream_or_string |
paulb@103 | 395 | return parseString(stream.read(), html) |
paulb@18 | 396 | else: |
paulb@103 | 397 | return parseFile(stream_or_string, html) |
paulb@32 | 398 | |
paulb@105 | 399 | def parseFile(filename, html=0): |
paulb@105 | 400 | |
paulb@105 | 401 | """ |
paulb@105 | 402 | Parse the file having the given 'filename'. If the optional 'html' parameter |
paulb@105 | 403 | is set to a true value, the content to be parsed will be treated as being |
paulb@105 | 404 | HTML rather than XML. |
paulb@105 | 405 | |
paulb@105 | 406 | A document object is returned by this function. |
paulb@105 | 407 | """ |
paulb@105 | 408 | |
paulb@105 | 409 | return Document(Node_parseFile(filename, html)) |
paulb@18 | 410 | |
paulb@103 | 411 | def parseString(s, html=0): |
paulb@105 | 412 | |
paulb@105 | 413 | """ |
paulb@105 | 414 | Parse the content of the given string 's'. If the optional 'html' parameter |
paulb@105 | 415 | is set to a true value, the content to be parsed will be treated as being |
paulb@105 | 416 | HTML rather than XML. |
paulb@105 | 417 | |
paulb@105 | 418 | A document object is returned by this function. |
paulb@105 | 419 | """ |
paulb@105 | 420 | |
paulb@103 | 421 | return Document(Node_parseString(s, html)) |
paulb@18 | 422 | |
paulb@103 | 423 | def parseURI(uri, html=0): |
paulb@105 | 424 | |
paulb@105 | 425 | """ |
paulb@105 | 426 | Parse the content found at the given 'uri'. If the optional 'html' parameter |
paulb@105 | 427 | is set to a true value, the content to be parsed will be treated as being |
paulb@105 | 428 | HTML rather than XML. |
paulb@128 | 429 | |
paulb@128 | 430 | The parseURI does not currently work with HTML. Use parse with a stream |
paulb@128 | 431 | object instead. For example: |
paulb@128 | 432 | |
paulb@128 | 433 | d = parse(urllib.urlopen("http://www.python.org"), html=1) |
paulb@105 | 434 | |
paulb@105 | 435 | A document object is returned by this function. |
paulb@105 | 436 | """ |
paulb@105 | 437 | |
paulb@103 | 438 | return Document(Node_parseURI(uri, html)) |
paulb@18 | 439 | |
paulb@120 | 440 | def toString(node, encoding=None, prettyprint=0): |
paulb@105 | 441 | |
paulb@105 | 442 | """ |
paulb@105 | 443 | Return a string containing the serialised form of the given 'node' and its |
paulb@105 | 444 | children. The optional 'encoding' can be used to override the default |
paulb@120 | 445 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 446 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 447 | setting). |
paulb@105 | 448 | """ |
paulb@105 | 449 | |
paulb@120 | 450 | return Node_toString(node.as_native_node(), encoding, prettyprint) |
paulb@18 | 451 | |
paulb@120 | 452 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@105 | 453 | |
paulb@105 | 454 | """ |
paulb@105 | 455 | Write the serialised form of the given 'node' and its children to the given |
paulb@105 | 456 | 'stream'. The optional 'encoding' can be used to override the default |
paulb@120 | 457 | character encoding used in the serialisation. The optional 'prettyprint' |
paulb@120 | 458 | indicates whether the serialised form is prettyprinted or not (the default |
paulb@120 | 459 | setting). |
paulb@105 | 460 | """ |
paulb@105 | 461 | |
paulb@120 | 462 | Node_toStream(node.as_native_node(), stream, encoding, prettyprint) |
paulb@72 | 463 | |
paulb@120 | 464 | def toFile(node, filename, encoding=None, prettyprint=0): |
paulb@105 | 465 | |
paulb@105 | 466 | """ |
paulb@105 | 467 | Write the serialised form of the given 'node' and its children to a file |
paulb@105 | 468 | having the given 'filename'. The optional 'encoding' can be used to override |
paulb@120 | 469 | the default character encoding used in the serialisation. The optional |
paulb@120 | 470 | 'prettyprint' indicates whether the serialised form is prettyprinted or not |
paulb@120 | 471 | (the default setting). |
paulb@105 | 472 | """ |
paulb@105 | 473 | |
paulb@120 | 474 | Node_toFile(node.as_native_node(), filename, encoding, prettyprint) |
paulb@18 | 475 | |
paulb@81 | 476 | def adoptNodes(nodes): |
paulb@105 | 477 | |
paulb@105 | 478 | """ |
paulb@105 | 479 | A special utility method which adopts the given low-level 'nodes' and which |
paulb@105 | 480 | returns a list of high-level equivalents. This is currently experimental and |
paulb@105 | 481 | should not be casually used. |
paulb@105 | 482 | """ |
paulb@105 | 483 | |
paulb@81 | 484 | if len(nodes) == 0: |
paulb@81 | 485 | return [] |
paulb@81 | 486 | doc = Document(libxml2mod.doc(nodes[0])) |
paulb@81 | 487 | results = [] |
paulb@81 | 488 | for node in nodes: |
paulb@81 | 489 | results.append(Node(node, doc)) |
paulb@81 | 490 | return results |
paulb@81 | 491 | |
paulb@18 | 492 | # vim: tabstop=4 expandtab shiftwidth=4 |