Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 2:40 AM   #1
tantric
Newbie
 
Join Date: Apr 2005
Posts: 2
Rep Power: 0 tantric is on a distinguished road
XML - changing textnodes with minidom

me: first time i saw python was a week ago. i'm trying to be ultraspecific in getting help....

objective: working with openRPG, which is written in python. you can insert xml docs in a certain format in openRPG. i have a character generator that makes chars in a different xml format. i want to take the "blank" openRPG-format xml sheet and insert the values from the chargen-format xlm sheet and i want to do it in python (the other option being perl, which i know a bit about)

status: reading in and storing the data using minidom tree-driven no problem, modifying the other sheet, total FUBAR. i know i should use event-driven, but i don't grok it yet....

example of blank xml sheet ....

          <row version="1.0">
            <cell>Name</cell>
            <cell/>
            <cell>Total Points</cell>
            <cell/>
          </row>

my code:
        if node.nodeValue and node.nodeValue == "Name":           
                print "found name node"
                print "name goes here", node.parentNode.nextSibling.firstChild
                namexmlstring = "<cell>" + "insert Name here" + "</cell>"
                newnamenode = parseString(namexmlstring)
                blanknamenode = node.parentNode.nextSibling.firstChild
                #things that don't work follow
                #tried many methods on many locations
                #blanknamenode = node.parentNode.nextSibling = newnamenode
                #node.parentNode.nextSibling = newnamenode
                #node.parentNode.appendChild(newnamenode) - doesn't work
                #node.parentNode._append_child(newnamenode) - doesn't work
                #node.parentNode.nextSibling.appendChild(newnamenode) - doesn't work
                #node.nextSibling.appendChild(newnamenode) - doesn't work is NoneType
                #node.parentNode.nextSibling._append_child(newnamenode) - doesn't work
                #blanknamenode.parentNode.replaceChild(blanknamenode, newnamenode) - no
                #blanknamenode.parentNode.appendChild(newnamenode) - no
                #node.appendChild(newnamenode)
                #node.parentNode.removeNode()

issue #1)what's up with the exception when an if test is undefined? why "if node.nodeValue and node.nodeValue == "Name": "? it has to be this way....

issue #2)why .appendChild AND ._append_Child? why don't the Node methods from minidom work on all nodes?

issue #3)how do i insert the idiot new node? when it does work (eg blanknamenode.replaceWholeText("yournamehere")), i keep getting:

<cell>Name</cell>yournamehere<cell/>

issue #4)somehow if i correct the blank sheet to

<cell>Name</cell>
<cell></cell>

issue #4 con't) it gets turned back the other way when i parsedxmlfile.toxml() it......

PS - yes, i'm a hack, it's tribute to python that i got as far as i did....
tantric is offline   Reply With Quote
Old Apr 26th, 2005, 1:45 PM   #2
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
It's easier to just showing you some working code. If you still have questions, repost it.
import xml.dom.minidom

# the xml contents
sheet = """\
<row version="1.0">
    <cell>Name</cell>
    <cell/>
    <cell>Total Points</cell>
    <cell/>
</row>"""

# get the document (or root) node
doc = xml.dom.minidom.parseString(sheet)

# get the first <row> element, then
# all its <cell> children.
row = doc.getElementsByTagName("row")[0]
cells  = row.getElementsByTagName("cell")

# find the index of the <cell> element
# that contains the "Name" text element.
for i in range(0, len(cells)):
    if (cells[i].firstChild != None and
        cells[i].firstChild.nodeType == doc.TEXT_NODE and
        cells[i].firstChild.data == "Name"):
        break

# create the new text node for the name
newtext = doc.createTextNode("MyName")

# add the new text in the next spot
# (you should really check to make sure
# the next child is really an empty <cell>
# node.)
cells[i+1].appendChild(newtext)

# print the new xml sheet
print row.toxml()
        
# clean up to free resources
doc.unlink()
Moldz is offline   Reply With Quote
Old Apr 26th, 2005, 5:30 PM   #3
tantric
Newbie
 
Join Date: Apr 2005
Posts: 2
Rep Power: 0 tantric is on a distinguished road
WOW

that is SO much better. domo arigato

technically, this is event-driven using minidom? you don't have to respond, i'm more than set.
tantric is offline   Reply With Quote
Old Apr 26th, 2005, 7:44 PM   #4
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
Nope, pure DOM using a "mini" or light implementation.
Moldz is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC