[XML-SIG] Creating an Entity reference

www.python.org at squirrelblue.org www.python.org at squirrelblue.org
Tue Oct 10 22:24:19 CEST 2006

On Tuesday 10 October 2006 19:24, Vines, John (Civ, ARL/CISD) wrote:
# 	I'm having a bit of an issue understanding how to create an
# entity .  My understanding is to create an instance of a document and
# use the "createEntityReference(name)" method to create the entity.
# from xml.dom import getDOMImplementation
# impl = getDOMImplementation()
# doctype = impl.createDocumentType( 'Xdmf', None, 'Xdmf.dtd' )
# self.xml_doc = impl.createDocument(None, "Xdmf", doctype)
# self.root = self.xml_doc.documentElement
# self.xml_doc.appendChild( self.root )  

On my system, getDOMImplementation returns the minidom implementation:
  Python 2.4.2 (#2, Nov 20 2005, 17:04:48) 
  [GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> from xml.dom import getDOMImplementation
  >>> impl = getDOMImplementation()
  >>> impl
  <xml.dom.minidom.DOMImplementation instance at 0xb7cf4d8c>

Which does not appear to have a createEntityReference() method:
  >>> doc = impl.createDocument(None, 'a', None)
  >>> [k for k in dir(doc) if k.startswith('create')]
  ['createAttribute', 'createAttributeNS', 'createCDATASection', 'createComment', 'createDocumentFragment', 'createElement', 'createElementNS', 'createProcessingInstruction', 'createTextNode']

The 4DOM implementation, however, does have such a method:
  >>> newImpl = getDOMImplementation('4DOM')
  >>> newImpl
  <xml.dom.DOMImplementation.DOMImplementation instance at 0xb7cf4f4c>
  >>> doc = newImpl.createDocument(None, 'a', None)
  >>> hasattr(doc, 'createEntityReference')

Which you use as one might expect:
  >>> lt = doc.createEntityReference('lt')
  >>> doc.documentElement.appendChild(lt)
  <EntityReference Node at -482df7b4: 'lt'>

  >>> from xml.dom.ext import Print # 4DOM does not have a toxml() method
  >>> from StringIO import StringIO
  >>> buf = StringIO()
  >>> Print(doc, buf)
  >>> buf.seek(0)
  >>> print buf.read()
  <?xml version='1.0' encoding='UTF-8'?><a>&lt;</a>

Another DOM implementation that you might be interested in is pxdom:

All these DOM implementations have their strengths and weaknesses.

Hope that helped,

More information about the XML-SIG mailing list