[XML-SIG] no parent?

Matthias Teege matthias-gmane at mteege.de
Tue Oct 12 10:41:12 CEST 2004


Moin,

I'm new to pyxml and try to replace a placeholder in a Openoffice file
with some data from a database. I use the following python code:

import xml.dom 
import xml.dom.ext
import xml.dom.minidom
import xml.parsers.expat
import sys
from zipfile import *
from StringIO import *

def fillData( filename ):
    dataSource = StringIO (inFile.read( filename ))
    tempFileName = "/tmp/workfile"
    dataSink = open(tempFileName, "w")
    document = xml.dom.minidom.parse( dataSource )
    elements = document.getElementsByTagName("text:placeholder")
    for element in elements:
        parent   = element.parentNode
        print element, parent
        children = parent.childNodes
        i = len(children)-1
        while (i >= 0):
            if (children[i].nodeName == "text:placeholder"):
                parent.removeChild( children[i] )
                # not remove, replace with text
            i = i - 1
    xml.dom.ext.Print( document, dataSink )

inFile = ZipFile( sys.argv[1] )
fillData('content.xml')
inFile.close

Because I'm new to XML I first try to remove the "text:placeholder"
nodes from the file. There are two placeholders in my sample file but
If I run the code I get the following:

<DOM Element: text:placeholder at 0x4063c50c> <DOM Element: text:p at 0x4063c4cc>
<DOM Element: text:placeholder at 0x4063c6ac> None
Traceback (most recent call last):
  File "./oo.py", line 32, in ?
    fillData('content.xml')
  File "./oo.py", line 22, in fillData
    children = parent.childNodes
AttributeError: 'NoneType' object has no attribute 'childNodes'

Where is the parent of the second placeholder? The relevant part from
content.xml looks like this:

...
<text:p text:style-name="Heading">Template</text:p>
<text:p text:style-name="Standard">This is some text but I need to replace 
<text:placeholder text:placeholder-type="text">&lt;Name&gt;</text:placeholder>
with some usefull text like 
<text:placeholder text:placeholder-type="text">&lt;usefull&gt;</text:placeholder>
. And here som more text.</text:p>
<text:p text:style-name="Standard"/>
</office:body>
</office:document-content>

Is there a better way to replace the complete placeholder tag with
usefull text?

Bis dann,
Matthias
-- 
Matthias Teege -- http://www.mteege.de
make world not war



More information about the XML-SIG mailing list