Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 31st, 2004, 02:35 PM
7imz 7imz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 7imz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
adding info to xml files

###########################

import xml.dom.minidom
from xml.dom.minidom import getDOMImplementation

output = open("book1.xml", "w")

impl = getDOMImplementation()
newdoc = impl.createDocument(None, "library", None)

book = newdoc.createElement("book")
newdoc.documentElement.appendChild(book)

chapter = newdoc.createElement("chapter")
book.appendChild(chapter)

page = newdoc.createElement("page")
pageContent = newdoc.createTextNode("somerandomtext")
page.appendChild(pageContent)
chapter.appendChild(page)

output.writelines(newdoc.toprettyxml())


#########################

after i create the xml file... how can i retrieve it and add some info to it... for example how can i add another page to chapter... or another chapter to book...any help would be greatly appreciated...

Reply With Quote
  #2  
Old April 1st, 2004, 04:49 AM
ilves ilves is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Norway
Posts: 41 ilves User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 22 sec
Reputation Power: 10
I think this code will add a chapter to your book.
Code:
import xml.dom.minidom

doc = xml.dom.minidom.parse("book1.xml")
book = doc.getElementsByTagName("book")[0]
newChapter = doc.createElement("chapter")
book.appendChild(newChapter)

newDoc = doc.toprettyxml()
fh = open("book1.xml", "w")
fh.write(newDoc)
fh.close()


There is also an insertBefore function you can use to insert the chapter before an existing chapter.
Code:
chapters = doc.getElementsByTagName("chapter")
newChapter = doc.createElement("chapter")
book.insertBefore(newChapter, chapters[3])

This will insert the new chapter before the 4th chapter.

There is one thing I don't like about the toprettyxml function. If you open a "pretty" xml document and prints it back to the file with toprettyxml, it adds the prettyfying whitespace one more time so you get twice as much whitespace as you want.
__________________
Good web hosting info - articles about web hosting
hb's web dev blog

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > adding info to xml files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap