paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@55 | 4 | DOM macros for virtual libxml2mod node methods and properties. |
paulb@46 | 5 | """ |
paulb@46 | 6 | |
paulb@46 | 7 | import xml.dom |
paulb@55 | 8 | import libxml2mod |
paulb@46 | 9 | |
paulb@46 | 10 | # NOTE: libxml2 seems to use UTF-8 throughout. |
paulb@46 | 11 | |
paulb@46 | 12 | def from_unicode(s): |
paulb@119 | 13 | if isinstance(s, unicode): |
paulb@46 | 14 | return s.encode("utf-8") |
paulb@46 | 15 | else: |
paulb@46 | 16 | return s |
paulb@46 | 17 | |
paulb@46 | 18 | def to_unicode(s): |
paulb@119 | 19 | if isinstance(s, str): |
paulb@46 | 20 | return unicode(s, encoding="utf-8") |
paulb@46 | 21 | else: |
paulb@46 | 22 | return s |
paulb@46 | 23 | |
paulb@46 | 24 | def _get_prefix_and_localName(name): |
paulb@46 | 25 | t = name.split(":") |
paulb@46 | 26 | if len(t) == 1: |
paulb@46 | 27 | return None, name |
paulb@46 | 28 | elif len(t) == 2: |
paulb@46 | 29 | return t |
paulb@46 | 30 | else: |
paulb@46 | 31 | # NOTE: Should raise an exception. |
paulb@46 | 32 | return None, None |
paulb@46 | 33 | |
paulb@46 | 34 | _nodeTypes = { |
paulb@46 | 35 | "attribute" : xml.dom.Node.ATTRIBUTE_NODE, |
paulb@46 | 36 | "comment" : xml.dom.Node.COMMENT_NODE, |
paulb@46 | 37 | "document_xml" : xml.dom.Node.DOCUMENT_NODE, |
paulb@46 | 38 | "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE, |
paulb@46 | 39 | "dtd" : xml.dom.Node.DOCUMENT_TYPE_NODE, # NOTE: Needs verifying. |
paulb@46 | 40 | "element" : xml.dom.Node.ELEMENT_NODE, |
paulb@46 | 41 | "entity" : xml.dom.Node.ENTITY_NODE, |
paulb@46 | 42 | "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE, |
paulb@46 | 43 | "notation" : xml.dom.Node.NOTATION_NODE, |
paulb@46 | 44 | "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE, |
paulb@46 | 45 | "text" : xml.dom.Node.TEXT_NODE |
paulb@46 | 46 | } |
paulb@46 | 47 | |
paulb@134 | 48 | _reverseNodeTypes = {} |
paulb@134 | 49 | for label, value in _nodeTypes.items(): |
paulb@134 | 50 | _reverseNodeTypes[value] = label |
paulb@134 | 51 | |
paulb@46 | 52 | def Node_ownerDocument(node): |
paulb@62 | 53 | return libxml2mod.doc(node) or node |
paulb@46 | 54 | |
paulb@46 | 55 | def Node_nodeType(node): |
paulb@55 | 56 | return _nodeTypes[libxml2mod.type(node)] |
paulb@46 | 57 | |
paulb@46 | 58 | def Node_childNodes(node): |
paulb@46 | 59 | |
paulb@46 | 60 | # NOTE: Consider a generator instead. |
paulb@46 | 61 | |
paulb@46 | 62 | child_nodes = [] |
paulb@55 | 63 | node = libxml2mod.children(node) |
paulb@46 | 64 | while node is not None: |
paulb@46 | 65 | child_nodes.append(node) |
paulb@55 | 66 | node = libxml2mod.next(node) |
paulb@46 | 67 | return child_nodes |
paulb@46 | 68 | |
paulb@46 | 69 | def Node_attributes(node): |
paulb@46 | 70 | attributes = {} |
paulb@55 | 71 | node = libxml2mod.properties(node) |
paulb@46 | 72 | while node is not None: |
paulb@55 | 73 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 74 | if ns is not None: |
paulb@55 | 75 | attributes[(libxml2mod.xmlNodeGetContent(ns), libxml2mod.name(node))] = node |
paulb@46 | 76 | else: |
paulb@55 | 77 | attributes[(None, libxml2mod.name(node))] = node |
paulb@55 | 78 | node = libxml2mod.next(node) |
paulb@46 | 79 | return attributes |
paulb@46 | 80 | |
paulb@46 | 81 | def Node_namespaceURI(node): |
paulb@55 | 82 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 83 | if ns is not None: |
paulb@55 | 84 | return to_unicode(libxml2mod.xmlNodeGetContent(ns)) |
paulb@46 | 85 | else: |
paulb@46 | 86 | return None |
paulb@46 | 87 | |
paulb@46 | 88 | def Node_nodeValue(node): |
paulb@55 | 89 | return to_unicode(libxml2mod.xmlNodeGetContent(node)) |
paulb@46 | 90 | |
paulb@87 | 91 | # NOTE: This is not properly exposed in the libxml2macro interface as the |
paulb@87 | 92 | # NOTE: writable form of nodeValue. |
paulb@87 | 93 | |
paulb@87 | 94 | def Node_setNodeValue(node, value): |
paulb@87 | 95 | # NOTE: Cannot set attribute node values. |
paulb@87 | 96 | libxml2mod.xmlNodeSetContent(node, from_unicode(value)) |
paulb@87 | 97 | |
paulb@73 | 98 | # NOTE: Verify this. |
paulb@73 | 99 | |
paulb@73 | 100 | Node_data = Node_nodeValue |
paulb@73 | 101 | |
paulb@46 | 102 | def Node_prefix(node): |
paulb@55 | 103 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 104 | if ns is not None: |
paulb@55 | 105 | return to_unicode(libxml2mod.name(ns)) |
paulb@46 | 106 | else: |
paulb@46 | 107 | return None |
paulb@46 | 108 | |
paulb@46 | 109 | def Node_nodeName(node): |
paulb@46 | 110 | prefix = Node_prefix(node) |
paulb@46 | 111 | if prefix is not None: |
paulb@46 | 112 | return prefix + ":" + Node_localName(node) |
paulb@46 | 113 | else: |
paulb@46 | 114 | return Node_localName(node) |
paulb@46 | 115 | |
paulb@46 | 116 | def Node_tagName(node): |
paulb@55 | 117 | if libxml2mod.type(node) == "element": |
paulb@46 | 118 | return Node_nodeName(node) |
paulb@46 | 119 | else: |
paulb@46 | 120 | return None |
paulb@46 | 121 | |
paulb@46 | 122 | def Node_localName(node): |
paulb@55 | 123 | return to_unicode(libxml2mod.name(node)) |
paulb@46 | 124 | |
paulb@46 | 125 | def Node_parentNode(node): |
paulb@55 | 126 | if libxml2mod.type(node) == "document_xml": |
paulb@46 | 127 | return None |
paulb@46 | 128 | else: |
paulb@55 | 129 | return libxml2mod.parent(node) |
paulb@46 | 130 | |
paulb@46 | 131 | def Node_previousSibling(node): |
paulb@55 | 132 | if libxml2mod.prev(node) is not None: |
paulb@55 | 133 | return libxml2mod.prev(node) |
paulb@46 | 134 | else: |
paulb@46 | 135 | return None |
paulb@46 | 136 | |
paulb@46 | 137 | def Node_nextSibling(node): |
paulb@55 | 138 | if libxml2mod.next(node) is not None: |
paulb@55 | 139 | return libxml2mod.next(node) |
paulb@46 | 140 | else: |
paulb@46 | 141 | return None |
paulb@46 | 142 | |
paulb@46 | 143 | def Node_hasAttributeNS(node, ns, localName): |
paulb@92 | 144 | return Node_getAttributeNS(node, ns, localName) is not None |
paulb@46 | 145 | |
paulb@46 | 146 | def Node_hasAttribute(node, name): |
paulb@92 | 147 | return Node_getAttribute(node, name) is not None |
paulb@46 | 148 | |
paulb@46 | 149 | def Node_getAttributeNS(node, ns, localName): |
paulb@55 | 150 | return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns)) |
paulb@46 | 151 | |
paulb@46 | 152 | def Node_getAttribute(node, name): |
paulb@55 | 153 | return to_unicode(libxml2mod.xmlGetProp(node, name)) |
paulb@46 | 154 | |
paulb@46 | 155 | def Node_getAttributeNodeNS(node, ns, localName): |
paulb@55 | 156 | # NOTE: Needs verifying. |
paulb@87 | 157 | return Node_attributes(node)[(ns, localName)] |
paulb@46 | 158 | |
paulb@46 | 159 | def Node_getAttributeNode(node, name): |
paulb@46 | 160 | # NOTE: Needs verifying. |
paulb@87 | 161 | return Node_attributes(node)[(None, name)] |
paulb@46 | 162 | |
paulb@46 | 163 | def Node_setAttributeNS(node, ns, name, value): |
paulb@46 | 164 | # NOTE: Need to convert from Unicode. |
paulb@46 | 165 | ns, name, value = map(from_unicode, [ns, name, value]) |
paulb@46 | 166 | |
paulb@46 | 167 | prefix, localName = _get_prefix_and_localName(name) |
paulb@55 | 168 | |
paulb@55 | 169 | # NOTE: Might need to be xmlSetNsProp. |
paulb@89 | 170 | if ns is not None and ns == libxml2mod.xmlNodeGetContent(libxml2mod.xmlNodeGetNs(node)): |
paulb@55 | 171 | libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNodeGetNs(node), localName, value) |
paulb@89 | 172 | elif prefix is not None: |
paulb@89 | 173 | new_ns = libxml2mod.xmlNewNs(node, ns, prefix) |
paulb@89 | 174 | libxml2mod.xmlNewNsProp(node, new_ns, localName, value) |
paulb@46 | 175 | else: |
paulb@46 | 176 | # NOTE: Needs verifying: what should happen to the namespace? |
paulb@46 | 177 | # NOTE: This also catches the case where None is the element's |
paulb@46 | 178 | # NOTE: namespace and is also used for the attribute. |
paulb@55 | 179 | libxml2mod.xmlNewNsProp(node, None, localName, value) |
paulb@46 | 180 | |
paulb@46 | 181 | def Node_setAttribute(node, name, value): |
paulb@46 | 182 | # NOTE: Need to convert from Unicode. |
paulb@46 | 183 | name, value = map(from_unicode, [name, value]) |
paulb@46 | 184 | |
paulb@55 | 185 | libxml2mod.xmlSetProp(node, name, value) |
paulb@55 | 186 | |
paulb@87 | 187 | def Node_setAttributeNodeNS(node, attr): |
paulb@87 | 188 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 189 | Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@55 | 190 | |
paulb@87 | 191 | def Node_setAttributeNode(node, attr): |
paulb@46 | 192 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 193 | Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@46 | 194 | |
paulb@92 | 195 | def Node_removeAttributeNS(node, ns, localName): |
paulb@92 | 196 | attr = Node_getAttributeNodeNS(node, ns, localName) |
paulb@92 | 197 | libxml2mod.xmlUnsetNsProp(node, libxml2mod.xmlNodeGetNs(attr), libxml2mod.name(attr)) |
paulb@92 | 198 | |
paulb@92 | 199 | def Node_removeAttribute(node, name): |
paulb@92 | 200 | name = from_unicode(name) |
paulb@92 | 201 | libxml2mod.xmlUnsetProp(node, name) |
paulb@92 | 202 | |
paulb@46 | 203 | def Node_createElementNS(node, ns, name): |
paulb@46 | 204 | # NOTE: Need to convert from Unicode. |
paulb@46 | 205 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 206 | |
paulb@46 | 207 | prefix, localName = _get_prefix_and_localName(name) |
paulb@55 | 208 | new_node = libxml2mod.xmlNewNode(localName) |
paulb@46 | 209 | # NOTE: Does it make sense to set the namespace if it is empty? |
paulb@46 | 210 | if ns is not None: |
paulb@55 | 211 | new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) |
paulb@55 | 212 | libxml2mod.xmlSetNs(new_node, new_ns) |
paulb@46 | 213 | return new_node |
paulb@46 | 214 | |
paulb@46 | 215 | def Node_createElement(node, name): |
paulb@46 | 216 | # NOTE: Need to convert from Unicode. |
paulb@46 | 217 | name = from_unicode(name) |
paulb@46 | 218 | |
paulb@55 | 219 | new_node = libxml2mod.xmlNewNode(name) |
paulb@46 | 220 | return new_node |
paulb@46 | 221 | |
paulb@46 | 222 | def Node_createAttributeNS(node, ns, name): |
paulb@87 | 223 | |
paulb@46 | 224 | # NOTE: Need to convert from Unicode. |
paulb@46 | 225 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 226 | |
paulb@46 | 227 | prefix, localName = _get_prefix_and_localName(name) |
paulb@46 | 228 | # NOTE: Does it make sense to set the namespace if it is empty? |
paulb@46 | 229 | if ns is not None: |
paulb@87 | 230 | new_ns = libxml2mod.xmlNewNs(node, ns, prefix) |
paulb@46 | 231 | else: |
paulb@46 | 232 | new_ns = None |
paulb@89 | 233 | new_node = libxml2mod.xmlNewNsProp(node, new_ns, localName, None) |
paulb@87 | 234 | return new_node |
paulb@46 | 235 | |
paulb@46 | 236 | def Node_createAttribute(node, name): |
paulb@87 | 237 | |
paulb@46 | 238 | # NOTE: Need to convert from Unicode. |
paulb@46 | 239 | name = from_unicode(name) |
paulb@46 | 240 | |
paulb@87 | 241 | # NOTE: xmlNewProp does not seem to work. |
paulb@87 | 242 | return Node_createAttributeNS(node, None, name) |
paulb@46 | 243 | |
paulb@46 | 244 | def Node_createTextNode(node, value): |
paulb@46 | 245 | # NOTE: Need to convert from Unicode. |
paulb@46 | 246 | value = from_unicode(value) |
paulb@46 | 247 | |
paulb@55 | 248 | return libxml2mod.xmlNewText(value) |
paulb@46 | 249 | |
paulb@46 | 250 | def Node_createComment(node, value): |
paulb@46 | 251 | # NOTE: Need to convert from Unicode. |
paulb@46 | 252 | value = from_unicode(value) |
paulb@46 | 253 | |
paulb@55 | 254 | return libxml2mod.xmlNewComment(value) |
paulb@46 | 255 | |
paulb@46 | 256 | def Node_insertBefore(node, tmp, oldNode): |
paulb@87 | 257 | return libxml2mod.xmlAddPrevSibling(oldNode, tmp) |
paulb@46 | 258 | |
paulb@46 | 259 | def Node_replaceChild(node, tmp, oldNode): |
paulb@87 | 260 | return libxml2mod.xmlReplaceNode(oldNode, tmp) |
paulb@46 | 261 | |
paulb@46 | 262 | def Node_appendChild(node, tmp): |
paulb@55 | 263 | return libxml2mod.xmlAddChild(node, tmp) |
paulb@46 | 264 | |
paulb@46 | 265 | def Node_removeChild(node, child): |
paulb@85 | 266 | libxml2mod.xmlUnlinkNode(child) |
paulb@46 | 267 | |
paulb@73 | 268 | def Node_importNode(node, other, deep): |
paulb@73 | 269 | if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 270 | imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other)) |
paulb@73 | 271 | for attr in Node_attributes(other).values(): |
paulb@73 | 272 | Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@73 | 273 | |
paulb@73 | 274 | if deep: |
paulb@73 | 275 | for child in Node_childNodes(other): |
paulb@73 | 276 | imported_child = Node_importNode(node, child, deep) |
paulb@73 | 277 | if imported_child: |
paulb@73 | 278 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 279 | |
paulb@73 | 280 | return imported_element |
paulb@73 | 281 | |
paulb@73 | 282 | elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE: |
paulb@73 | 283 | return Node_createTextNode(node, Node_nodeValue(other)) |
paulb@73 | 284 | |
paulb@73 | 285 | elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 286 | return Node_createComment(node, Node_data(other)) |
paulb@73 | 287 | |
paulb@134 | 288 | raise ValueError, "Node type '%s' (%d) not supported." % (other, Node_nodeType(other)) |
paulb@73 | 289 | |
paulb@73 | 290 | def Node_importNode_DOM(node, other, deep): |
paulb@73 | 291 | if other.nodeType == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 292 | imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName) |
paulb@73 | 293 | for attr in other.attributes.values(): |
paulb@73 | 294 | Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue) |
paulb@73 | 295 | |
paulb@73 | 296 | if deep: |
paulb@73 | 297 | for child in other.childNodes: |
paulb@73 | 298 | imported_child = Node_importNode_DOM(node, child, deep) |
paulb@73 | 299 | if imported_child: |
paulb@73 | 300 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 301 | |
paulb@73 | 302 | return imported_element |
paulb@73 | 303 | |
paulb@73 | 304 | elif other.nodeType == xml.dom.Node.TEXT_NODE: |
paulb@73 | 305 | return Node_createTextNode(node, other.nodeValue) |
paulb@73 | 306 | |
paulb@73 | 307 | elif other.nodeType == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 308 | return Node_createComment(node, other.data) |
paulb@73 | 309 | |
paulb@134 | 310 | raise ValueError, "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType) |
paulb@73 | 311 | |
paulb@46 | 312 | def Node_xpath(node, expr, variables=None, namespaces=None): |
paulb@55 | 313 | context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node)) |
paulb@82 | 314 | libxml2mod.xmlXPathSetContextNode(context, node) |
paulb@46 | 315 | # NOTE: Discover namespaces from the node. |
paulb@97 | 316 | # NOTE: Work out how to specify paths without having to use prefixes on |
paulb@97 | 317 | # NOTE: names all the time. |
paulb@46 | 318 | for prefix, ns in (namespaces or {}).items(): |
paulb@55 | 319 | libxml2mod.xmlXPathRegisterNs(context, prefix, ns) |
paulb@97 | 320 | # NOTE: No such functions are exposed in current versions of libxml2. |
paulb@97 | 321 | #for (prefix, ns), value in (variables or {}).items(): |
paulb@97 | 322 | # libxml2mod.xmlXPathRegisterVariableNS(context, prefix, ns, value) |
paulb@77 | 323 | result = libxml2mod.xmlXPathEval(expr, context) |
paulb@77 | 324 | libxml2mod.xmlXPathFreeContext(context) |
paulb@77 | 325 | return result |
paulb@46 | 326 | |
paulb@46 | 327 | # Utility functions. |
paulb@46 | 328 | |
paulb@46 | 329 | def createDocumentType(localName, publicId, systemId): |
paulb@46 | 330 | return None |
paulb@46 | 331 | |
paulb@46 | 332 | def createDocument(namespaceURI, localName, doctype): |
paulb@46 | 333 | # NOTE: Fixed to use version 1.0 only. |
paulb@55 | 334 | d = libxml2mod.xmlNewDoc("1.0") |
paulb@46 | 335 | if localName is not None: |
paulb@46 | 336 | root = Node_createElementNS(d, namespaceURI, localName) |
paulb@46 | 337 | Node_appendChild(d, root) |
paulb@46 | 338 | return d |
paulb@46 | 339 | |
paulb@103 | 340 | def parse(stream_or_string, html=0): |
paulb@46 | 341 | if hasattr(stream_or_string, "read"): |
paulb@46 | 342 | stream = stream_or_string |
paulb@103 | 343 | return parseString(stream.read(), html) |
paulb@46 | 344 | else: |
paulb@103 | 345 | return parseFile(stream_or_string, html) |
paulb@46 | 346 | |
paulb@103 | 347 | def parseFile(s, html=0): |
paulb@46 | 348 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 349 | if not html: |
paulb@103 | 350 | context = libxml2mod.xmlCreateFileParserCtxt(s) |
paulb@106 | 351 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@103 | 352 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@103 | 353 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@103 | 354 | libxml2mod.xmlParseDocument(context) |
paulb@103 | 355 | return libxml2mod.xmlParserGetDoc(context) |
paulb@103 | 356 | else: |
paulb@103 | 357 | return libxml2mod.htmlReadFile(s, None, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) |
paulb@46 | 358 | |
paulb@103 | 359 | def parseString(s, html=0): |
paulb@46 | 360 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 361 | if not html: |
paulb@103 | 362 | context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) |
paulb@106 | 363 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@103 | 364 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@103 | 365 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@103 | 366 | libxml2mod.xmlParseDocument(context) |
paulb@103 | 367 | return libxml2mod.xmlParserGetDoc(context) |
paulb@103 | 368 | else: |
paulb@103 | 369 | # NOTE: URL given as None. |
paulb@103 | 370 | html_url = None |
paulb@103 | 371 | return libxml2mod.htmlReadMemory(s, len(s), html_url, None, |
paulb@103 | 372 | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) |
paulb@46 | 373 | |
paulb@103 | 374 | def parseURI(uri, html=0): |
paulb@103 | 375 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 376 | if not html: |
paulb@116 | 377 | context = libxml2mod.xmlCreateURLParserCtxt(uri, 0) |
paulb@106 | 378 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@103 | 379 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@103 | 380 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@103 | 381 | libxml2mod.xmlParseDocument(context) |
paulb@103 | 382 | return libxml2mod.xmlParserGetDoc(context) |
paulb@103 | 383 | else: |
paulb@127 | 384 | raise NotImplementedError, "parseURI does not yet support HTML" |
paulb@55 | 385 | |
paulb@120 | 386 | def toString(node, encoding=None, prettyprint=0): |
paulb@120 | 387 | return libxml2mod.serializeNode(node, encoding, prettyprint) |
paulb@73 | 388 | |
paulb@120 | 389 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@120 | 390 | stream.write(toString(node, encoding, prettyprint)) |
paulb@73 | 391 | |
paulb@120 | 392 | def toFile(node, f, encoding=None, prettyprint=0): |
paulb@120 | 393 | libxml2mod.saveNodeTo(node, f, encoding, prettyprint) |
paulb@46 | 394 | |
paulb@103 | 395 | # libxml2mod constants. |
paulb@103 | 396 | |
paulb@103 | 397 | HTML_PARSE_NOERROR = 32 |
paulb@103 | 398 | HTML_PARSE_NOWARNING = 64 |
paulb@103 | 399 | HTML_PARSE_NONET = 2048 |
paulb@103 | 400 | XML_PARSE_NOERROR = 32 |
paulb@103 | 401 | XML_PARSE_NOWARNING = 64 |
paulb@103 | 402 | XML_PARSE_NONET = 2048 |
paulb@103 | 403 | |
paulb@46 | 404 | # vim: tabstop=4 expandtab shiftwidth=4 |