[Zope] XMLDocument Enhancement

James W. Howe jwh@allencreek.com
Fri, 04 Feb 2000 11:27:39 -0500


I posted a question recently about whether an XMLDocument could render the 
entire content of a specific XML tag.  The situation I encountered involved 
the inclusion of HTML-ish markup (<p>, <br>, etc.) contained inside of 
another tag.  For example:

<entry>
      <question><p>To be or not to be?</p></question>
</entry>

Using the text_content('question') method resulted in no content being 
returned.  In looking at the Node object, I noticed a method called 
toXML.  Turns out that toXML is almost what I want, except that it produces 
the XML for the entire object.  Using toXML and text_content as examples, I 
added the following code to the Node class in Node.py to facilitate 
returning XML content for a specific child node (one level deep):

     def nodeToXML(self, name=None, RESPONSE=None):
         """
         Answer the XML for the specified child node name.  If no node is 
specified, answer the XML
         for the entire node.
         """
         if name is None:
         	return self.toXML(self, 1, RESPONSE)
         else:
         	result = ""
         	for child in self:
         		if child.getNodeName() == name:
         			result = result + child.toXML()
         	return result

Used on XML such as above, the result of doing nodeToXML('question') would 
be "<p>To be or not to be?</p>"

James W. Howe				mailto:jwh@allencreek.com
Allen Creek Software, Inc.		pgpkey: http://ic.net/~jwh/pgpkey.html		
Ann Arbor, MI  48103