XML parsing and writing

C

c00i90wn

Hey, I'm having a problem with the xml.dom.minidom package, I want to
generate a simple xml for storing configuration variables, for that
purpose I've written the following code, but before pasting it I'll
tell you what my problem is. On first write of the xml everything goes
as it should but on subsequent writes it starts to add more and more
unneeded newlines to it making it hard to read and ugly. Here is the
code that you can test by yourself:

from xml.dom import minidom
import os

class config(object):
def __init__(self):
self.xml = os.path.abspath("conf\config.xml")
if not os.path.isfile(self.xml):
self.doc = minidom.Document()
self.__createFile()
else:
self.doc = minidom.parse(self.xml)
self.conf = self.doc.getElementsByTagName("conf")[0]

def __createFile(self):
self.conf = self.doc.createElement("conf")
self.doc.appendChild(self.conf)
self.write()

def createNode(self, element, data=False):
if len(self.conf.getElementsByTagName(str(element))) == 0:
newNode = self.doc.createElement(str(element))
self.conf.appendChild(newNode)
if data: self.nodeData(element, data)

def nodeData(self, node, data=False):
try:
node = self.conf.getElementsByTagName(node)[0]
except IndexError:
return
if data:
if node.hasChildNodes():
node.firstChild.replaceWholeText(str(data))
else:
data = self.doc.createTextNode(str(data))
node.appendChild(data)
else:
return node.firstChild.data

def write(self):
print self.conf.toprettyxml()
self.doc.writexml(open(self.xml, "w"), addindent = " ", newl =
"\n", encoding = "utf-8")

if __name__ == "__main__":
conf = config()
conf.createNode("path")
conf.nodeData("path", "somepath")
print conf.nodeData("path")
conf.createNode("blah", "foo")
print conf.nodeData("blah")
conf.write()
 
S

Stefan Behnel

c00i90wn said:
Hey, I'm having a problem with the xml.dom.minidom package, I want to
generate a simple xml for storing configuration variables, for that
purpose I've written the following code, but before pasting it I'll
tell you what my problem is. On first write of the xml everything goes
as it should but on subsequent writes it starts to add more and more
unneeded newlines to it making it hard to read and ugly.

Maybe you should try to get your code a little cleaner first, that usually
helps in finding these kinds of bugs. Try rewriting it with ElementTree or
lxml, that usually helps you in getting your work done.

http://effbot.org/zone/element-index.htm
http://codespeak.net/lxml/

Stefan
 
C

c00i90wn

Nice package ElementTree is but sadly it doesn't have a pretty print,
well, guess I'll have to do it myself, if you have one already can you
please give it to me? thanks :)
 
J

Jim

c00i90wn wrote:
On first write of the xml everything goes
as it should but on subsequent writes it starts to add more and more
unneeded newlines to it making it hard to read and ugly.
Pretty make it pretty by putting in newlines (and spaces) that are not
in the original data. That is, if you have text "John Smith"
associated with the element <name> then pretty gives you something like

<name>
John Smith
</name>
here with an extra two newlines and some whitespace indentation. (I
don't recall 100% when it puts in stuff, but the point of pretty is to
put in extra stuff.) You need to strip out the extra stuff (or print
it out not pretty; can you get a viewer that buffs-up a notbuff file so
you are seeing pretty but the data isn't actually pretty?).

Jim
 
S

Stefan Behnel

c00i90wn said:
Nice package ElementTree is but sadly it doesn't have a pretty print,
well, guess I'll have to do it myself, if you have one already can you
please give it to me? thanks :)

lxml's output functions all accept a "pretty_print" keyword argument.

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top