1.1 --- a/libxml2dom/macrolib/macrolib.py Sat May 14 23:30:46 2005 +0000
1.2 +++ b/libxml2dom/macrolib/macrolib.py Sat May 14 23:32:41 2005 +0000
1.3 @@ -95,6 +95,10 @@
1.4 def Node_nodeValue(node):
1.5 return to_unicode(libxml2mod.xmlNodeGetContent(node))
1.6
1.7 +# NOTE: Verify this.
1.8 +
1.9 +Node_data = Node_nodeValue
1.10 +
1.11 def Node_prefix(node):
1.12 ns = libxml2mod.xmlNodeGetNs(node)
1.13 if ns is not None:
1.14 @@ -264,6 +268,50 @@
1.15 def Node_removeChild(node, child):
1.16 libxml2mod.unlinkNode(child)
1.17
1.18 +def Node_importNode(node, other, deep):
1.19 + if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE:
1.20 + imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other))
1.21 + for attr in Node_attributes(other).values():
1.22 + Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr))
1.23 +
1.24 + if deep:
1.25 + for child in Node_childNodes(other):
1.26 + imported_child = Node_importNode(node, child, deep)
1.27 + if imported_child:
1.28 + Node_appendChild(imported_element, imported_child)
1.29 +
1.30 + return imported_element
1.31 +
1.32 + elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE:
1.33 + return Node_createTextNode(node, Node_nodeValue(other))
1.34 +
1.35 + elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE:
1.36 + return Node_createComment(node, Node_data(other))
1.37 +
1.38 + raise ValueError, Node_nodeType(other)
1.39 +
1.40 +def Node_importNode_DOM(node, other, deep):
1.41 + if other.nodeType == xml.dom.Node.ELEMENT_NODE:
1.42 + imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName)
1.43 + for attr in other.attributes.values():
1.44 + Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue)
1.45 +
1.46 + if deep:
1.47 + for child in other.childNodes:
1.48 + imported_child = Node_importNode_DOM(node, child, deep)
1.49 + if imported_child:
1.50 + Node_appendChild(imported_element, imported_child)
1.51 +
1.52 + return imported_element
1.53 +
1.54 + elif other.nodeType == xml.dom.Node.TEXT_NODE:
1.55 + return Node_createTextNode(node, other.nodeValue)
1.56 +
1.57 + elif other.nodeType == xml.dom.Node.COMMENT_NODE:
1.58 + return Node_createComment(node, other.data)
1.59 +
1.60 + raise ValueError, other.nodeType
1.61 +
1.62 def Node_xpath(node, expr, variables=None, namespaces=None):
1.63 context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node))
1.64 # NOTE: Discover namespaces from the node.
1.65 @@ -315,7 +363,13 @@
1.66 libxml2mod.xmlParseDocument(context)
1.67 return libxml2mod.xmlParserGetDoc(context)
1.68
1.69 -def toFile(doc, s):
1.70 - libxml2mod.xmlSaveFile(s, doc)
1.71 +def toString(node, encoding=None):
1.72 + return libxml2mod.serializeNode(node, encoding, 0)
1.73 +
1.74 +def toStream(node, stream, encoding=None):
1.75 + stream.write(toString(node, encoding))
1.76 +
1.77 +def toFile(node, f, encoding=None):
1.78 + libxml2mod.saveNodeTo(node, f, encoding, 0)
1.79
1.80 # vim: tabstop=4 expandtab shiftwidth=4