A couple of changes to make this more conformant. MvL and Uche agree.
This will make it incompatible with the version found in Python 2.0. Does this need to be done to PyXML too? Changes that might break existing code are marked with (!) below. - Formatting nit: no spaces inside parentheses: foo( a ) -> foo(a). - Break long lines. - (!) Fix getAttribute() and getAttributeNS() to return "" instead of raising KeyError when the attribute is not found. - (!) Fix getAttributeNodeNS() to return None instead of raising KeyError. (Curiously, getAttributeNode() already did this.) - Added hasAttributes(), which returns true iff the node has any attributes. )This is DOM level 3.) - (!) In createDocument(), if the qualified name is not empty, actually create and insert the first element with that name (this will become doc.documentElement). MvL believes that it should be an error to specify an empty qualified name; I'm not going there today, since it would require making a matching change to pulldom. Maybe MvL will do this. - In Document.writexml(), insert an xml declaration at the top. (This doesn't include the encoding since there's no way to specify the encoding. If that's preferred, all writexml() methods should be fixed to support an optional encoding argument that they pass to each other -- and they should use it to encode all text they write, too. Later.)
This commit is contained in:
parent
795ad56b31
commit
9e1fe1ec67
|
@ -1,9 +1,9 @@
|
|||
"""\
|
||||
minidom.py -- a lightweight DOM implementation.
|
||||
|
||||
parse( "foo.xml" )
|
||||
parse("foo.xml")
|
||||
|
||||
parseString( "<foo><bar/></foo>" )
|
||||
parseString("<foo><bar/></foo>")
|
||||
|
||||
Todo:
|
||||
=====
|
||||
|
@ -47,7 +47,7 @@ class Node(_Node):
|
|||
Node.allnodes[index] = repr(self.__dict__)
|
||||
if Node.debug is None:
|
||||
Node.debug = _get_StringIO()
|
||||
#open( "debug4.out", "w" )
|
||||
#open("debug4.out", "w")
|
||||
Node.debug.write("create %s\n" % index)
|
||||
|
||||
def __getattr__(self, key):
|
||||
|
@ -102,7 +102,7 @@ class Node(_Node):
|
|||
def insertBefore(self, newChild, refChild):
|
||||
if newChild.nodeType not in self.childNodeTypes:
|
||||
raise HierarchyRequestErr, \
|
||||
"%s cannot be child of %s" % (repr(newChild), repr(self) )
|
||||
"%s cannot be child of %s" % (repr(newChild), repr(self))
|
||||
if newChild.parentNode is not None:
|
||||
newChild.parentNode.removeChild(newChild)
|
||||
if refChild is None:
|
||||
|
@ -125,7 +125,7 @@ class Node(_Node):
|
|||
def appendChild(self, node):
|
||||
if node.nodeType not in self.childNodeTypes:
|
||||
raise HierarchyRequestErr, \
|
||||
"%s cannot be child of %s" % (repr(node), repr(self) )
|
||||
"%s cannot be child of %s" % (repr(node), repr(self))
|
||||
if node.parentNode is not None:
|
||||
node.parentNode.removeChild(node)
|
||||
if self.childNodes:
|
||||
|
@ -143,7 +143,7 @@ class Node(_Node):
|
|||
def replaceChild(self, newChild, oldChild):
|
||||
if newChild.nodeType not in self.childNodeTypes:
|
||||
raise HierarchyRequestErr, \
|
||||
"%s cannot be child of %s" % (repr(newChild), repr(self) )
|
||||
"%s cannot be child of %s" % (repr(newChild), repr(self))
|
||||
if newChild.parentNode is not None:
|
||||
newChild.parentNode.removeChild(newChild)
|
||||
if newChild is oldChild:
|
||||
|
@ -435,10 +435,16 @@ class Element(Node):
|
|||
Node.unlink(self)
|
||||
|
||||
def getAttribute(self, attname):
|
||||
try:
|
||||
return self._attrs[attname].value
|
||||
except KeyError:
|
||||
return ""
|
||||
|
||||
def getAttributeNS(self, namespaceURI, localName):
|
||||
try:
|
||||
return self._attrsNS[(namespaceURI, localName)].value
|
||||
except KeyError:
|
||||
return ""
|
||||
|
||||
def setAttribute(self, attname, value):
|
||||
attr = Attr(attname)
|
||||
|
@ -457,7 +463,7 @@ class Element(Node):
|
|||
return self._attrs.get(attrname)
|
||||
|
||||
def getAttributeNodeNS(self, namespaceURI, localName):
|
||||
return self._attrsNS[(namespaceURI, localName)]
|
||||
return self._attrsNS.get((namespaceURI, localName))
|
||||
|
||||
def setAttributeNode(self, attr):
|
||||
if attr.ownerElement not in (None, self):
|
||||
|
@ -528,6 +534,12 @@ class Element(Node):
|
|||
def _get_attributes(self):
|
||||
return AttributeList(self._attrs, self._attrsNS)
|
||||
|
||||
def hasAttributes(self):
|
||||
if self._attrs or self._attrsNS:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
class Comment(Node):
|
||||
nodeType = Node.COMMENT_NODE
|
||||
nodeName = "#comment"
|
||||
|
@ -624,7 +636,8 @@ class DOMImplementation:
|
|||
|
||||
def createDocument(self, namespaceURI, qualifiedName, doctype):
|
||||
if doctype and doctype.parentNode is not None:
|
||||
raise xml.dom.WrongDocumentErr("doctype object owned by another DOM tree")
|
||||
raise xml.dom.WrongDocumentErr(
|
||||
"doctype object owned by another DOM tree")
|
||||
doc = Document()
|
||||
if doctype is None:
|
||||
doctype = self.createDocumentType(qualifiedName, None, None)
|
||||
|
@ -634,7 +647,11 @@ class DOMImplementation:
|
|||
and namespaceURI != "http://www.w3.org/XML/1998/namespace":
|
||||
raise xml.dom.NamespaceErr("illegal use of 'xml' prefix")
|
||||
if prefix and not namespaceURI:
|
||||
raise xml.dom.NamespaceErr("illegal use of prefix without namespaces")
|
||||
raise xml.dom.NamespaceErr(
|
||||
"illegal use of prefix without namespaces")
|
||||
element = doc.createElementNS(namespaceURI, qualifiedName)
|
||||
doc.appendChild(element)
|
||||
# XXX else, raise an error? Empty qname is illegal in the DOM spec!
|
||||
doctype.parentNode = doc
|
||||
doc.doctype = doctype
|
||||
doc.implementation = self
|
||||
|
@ -662,13 +679,14 @@ class Document(Node):
|
|||
def appendChild(self, node):
|
||||
if node.nodeType not in self.childNodeTypes:
|
||||
raise HierarchyRequestErr, \
|
||||
"%s cannot be child of %s" % (repr(node), repr(self) )
|
||||
"%s cannot be child of %s" % (repr(node), repr(self))
|
||||
if node.parentNode is not None:
|
||||
node.parentNode.removeChild(node)
|
||||
|
||||
if node.nodeType == Node.ELEMENT_NODE \
|
||||
and self._get_documentElement():
|
||||
raise xml.dom.HierarchyRequestErr("two document elements disallowed")
|
||||
raise xml.dom.HierarchyRequestErr(
|
||||
"two document elements disallowed")
|
||||
return Node.appendChild(self, node)
|
||||
|
||||
def removeChild(self, oldChild):
|
||||
|
@ -720,6 +738,7 @@ class Document(Node):
|
|||
return rc
|
||||
|
||||
def writexml(self, writer):
|
||||
writer.write('<?xml version="1.0" ?>\n')
|
||||
for node in self.childNodes:
|
||||
node.writexml(writer)
|
||||
|
||||
|
|
Loading…
Reference in New Issue