![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
XML minidom... help
I have been programming in python off and on and I am trying to really sit down and get more proficient in it this summer.
Right now I am trying to make a basic program for reading, modifying and writing data with an XML file. I am using minidom and I and here is the problem I am running into. Say I have a game inventory or something like this: <Inv> <Sword> <Name>Broad Sword</Name> </Sword> <Shield> <Name>Big Shield</Name> </Shield> </Inv> I want to return the Name section of Shield. If I use a function like "getElementsByTagName" then it will return a list containing the contents of all Name sections. How do I explicitly tell minidom to get an element in... say this order Inv>Shield>Name? I tried setting it up so it loaded the elements in that order i.e I loaded Inv into a variable then I loaded Shield and finally Name. That works fine if the document is small, but obviously that's inefficient, and I cant save the file if I choose to modify an element. I'm sure there is something simple that I am forgetting or that I missed(there always seems to be in python). Can someone please steer me in the right direction.
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!" |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
python Syntax (Toggle Plain Text)
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
Well what you posted is essentially what I did do read data from an XML file. However my problem really is about modifying the file. Say I want to change the "Broad Sword" to "Short Sword" or add another element to my XML file. I could do it that way, but wouldn't the changes just take place to the code that was in the variable? How do I modify and save the document.
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!" |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Well, say you have found the element you want via the approach demonstrated above, and assigned it to the variable "sword":
python Syntax (Toggle Plain Text)
However, this isn't a very good way of doing things. A better way would be to have a "dump" and "load" method on each class: python Syntax (Toggle Plain Text)
python Syntax (Toggle Plain Text)
python Syntax (Toggle Plain Text)
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
Ok I see, thank you very much for the informative post
![]()
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding nodes with minidom | MegaArcon | Python | 4 | Feb 21st, 2006 12:37 PM |
| XML - changing textnodes with minidom | tantric | Python | 3 | Apr 26th, 2005 6:44 PM |