1.1 --- a/libxml2dom/__init__.py Mon Aug 25 22:30:19 2008 +0200
1.2 +++ b/libxml2dom/__init__.py Tue Aug 26 00:17:04 2008 +0200
1.3 @@ -19,7 +19,7 @@
1.4 with this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -__version__ = "0.4.6"
1.8 +__version__ = "0.4.7"
1.9
1.10 from libxml2dom.macrolib import *
1.11 from libxml2dom.macrolib import \
1.12 @@ -29,7 +29,6 @@
1.13 toString as Node_toString, toStream as Node_toStream, \
1.14 toFile as Node_toFile
1.15 import urllib # for parseURI in HTML mode
1.16 -import xml.dom # for getElementById
1.17
1.18 # Standard namespaces.
1.19
1.20 @@ -427,12 +426,11 @@
1.21 return tmp
1.22
1.23 def getElementById(self, identifier):
1.24 - nodes = self.xpath(".//*[@xml:id='" + identifier.replace("'", "'") + "']",
1.25 - namespaces={"xml" : xml.dom.XML_NAMESPACE})
1.26 - if nodes:
1.27 - return nodes[0]
1.28 + _node = Node_getElementById(self.ownerDocument.as_native_node(), identifier)
1.29 + if _node is None:
1.30 + return None
1.31 else:
1.32 - return None
1.33 + return self.impl.get_node(_node, self)
1.34
1.35 def getElementsByTagName(self, tagName):
1.36 return self.xpath(".//" + tagName)
1.37 @@ -619,7 +617,7 @@
1.38 def createDocument(namespaceURI, localName, doctype):
1.39 return default_impl.createDocument(namespaceURI, localName, doctype)
1.40
1.41 -def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None):
1.42 +def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None):
1.43
1.44 """
1.45 Parse the given 'stream_or_string', where the supplied object can either be
1.46 @@ -636,6 +634,13 @@
1.47 documents will be parsed, even though such documents may be missing content
1.48 such as closing tags.
1.49
1.50 + If the optional 'validate' parameter is set to a true value, an attempt will
1.51 + be made to validate the parsed document.
1.52 +
1.53 + If the optional 'remote' parameter is set to a true value, references to
1.54 + remote documents (such as DTDs) will be followed in order to obtain such
1.55 + documents.
1.56 +
1.57 A document object is returned by this function.
1.58 """
1.59
1.60 @@ -643,11 +648,13 @@
1.61
1.62 if hasattr(stream_or_string, "read"):
1.63 stream = stream_or_string
1.64 - return parseString(stream.read(), html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=impl)
1.65 + return parseString(stream.read(), html=html, htmlencoding=htmlencoding,
1.66 + unfinished=unfinished, validate=validate, remote=remote, impl=impl)
1.67 else:
1.68 - return parseFile(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=impl)
1.69 + return parseFile(stream_or_string, html=html, htmlencoding=htmlencoding,
1.70 + unfinished=unfinished, validate=validate, remote=remote, impl=impl)
1.71
1.72 -def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None):
1.73 +def parseFile(filename, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None):
1.74
1.75 """
1.76 Parse the file having the given 'filename'. The optional parameters
1.77 @@ -662,13 +669,21 @@
1.78 documents will be parsed, even though such documents may be missing content
1.79 such as closing tags.
1.80
1.81 + If the optional 'validate' parameter is set to a true value, an attempt will
1.82 + be made to validate the parsed document.
1.83 +
1.84 + If the optional 'remote' parameter is set to a true value, references to
1.85 + remote documents (such as DTDs) will be followed in order to obtain such
1.86 + documents.
1.87 +
1.88 A document object is returned by this function.
1.89 """
1.90
1.91 impl = impl or default_impl
1.92 - return impl.adoptDocument(Node_parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished))
1.93 + return impl.adoptDocument(Node_parseFile(filename, html=html, htmlencoding=htmlencoding,
1.94 + unfinished=unfinished, validate=validate, remote=remote))
1.95
1.96 -def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None):
1.97 +def parseString(s, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None):
1.98
1.99 """
1.100 Parse the content of the given string 's'. The optional parameters described
1.101 @@ -683,13 +698,21 @@
1.102 documents will be parsed, even though such documents may be missing content
1.103 such as closing tags.
1.104
1.105 + If the optional 'validate' parameter is set to a true value, an attempt will
1.106 + be made to validate the parsed document.
1.107 +
1.108 + If the optional 'remote' parameter is set to a true value, references to
1.109 + remote documents (such as DTDs) will be followed in order to obtain such
1.110 + documents.
1.111 +
1.112 A document object is returned by this function.
1.113 """
1.114
1.115 impl = impl or default_impl
1.116 - return impl.adoptDocument(Node_parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished))
1.117 + return impl.adoptDocument(Node_parseString(s, html=html, htmlencoding=htmlencoding,
1.118 + unfinished=unfinished, validate=validate, remote=remote))
1.119
1.120 -def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None):
1.121 +def parseURI(uri, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0, impl=None):
1.122
1.123 """
1.124 Parse the content found at the given 'uri'. The optional parameters
1.125 @@ -704,6 +727,13 @@
1.126 documents will be parsed, even though such documents may be missing content
1.127 such as closing tags.
1.128
1.129 + If the optional 'validate' parameter is set to a true value, an attempt will
1.130 + be made to validate the parsed document.
1.131 +
1.132 + If the optional 'remote' parameter is set to a true value, references to
1.133 + remote documents (such as DTDs) will be followed in order to obtain such
1.134 + documents.
1.135 +
1.136 XML documents are retrieved using libxml2's own network capabilities; HTML
1.137 documents are retrieved using the urllib module provided by Python. To
1.138 retrieve either kind of document using Python's own modules for this purpose
1.139 @@ -721,12 +751,14 @@
1.140 if html:
1.141 f = urllib.urlopen(uri)
1.142 try:
1.143 - return parse(f, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=impl)
1.144 + return parse(f, html=html, htmlencoding=htmlencoding, unfinished=unfinished,
1.145 + validate=validate, remote=remote, impl=impl)
1.146 finally:
1.147 f.close()
1.148 else:
1.149 impl = impl or default_impl
1.150 - return impl.adoptDocument(Node_parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished))
1.151 + return impl.adoptDocument(Node_parseURI(uri, html=html, htmlencoding=htmlencoding,
1.152 + unfinished=unfinished, validate=validate, remote=remote))
1.153
1.154 def toString(node, encoding=None, prettyprint=0):
1.155