![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 2
Rep Power: 0
![]() |
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.... |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
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() |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 2
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
Nope, pure DOM using a "mini" or light implementation.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|