473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XML parsing and writing

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()

Jul 28 '06 #1
6 1969
c00i90wn wrote:
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
Jul 31 '06 #2
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 :)

Stefan Behnel wrote:
c00i90wn wrote:
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
Jul 31 '06 #3
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 <namethen 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

Aug 1 '06 #4
c00i90wn wrote:
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 :)
FWIW Amara and plain old 4Suite both support pretty-print, canonical
XML print and more such options.

http://uche.ogbuji.net/tech/4suite/amara/
http://4Suite.org

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Aug 28 '06 #5
c00i90wn wrote:
Stefan Behnel wrote:
>c00i90wn wrote:
>>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/

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
Aug 29 '06 #6
someone wrote:
>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 :)
http://effbot.python-hosting.com/fil...tlib/indent.py

</F>

Aug 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Scott | last post by:
I have a program that listens on an IRC channel and logs everything to XML on standard output. The format of the XML is pretty straightforward, looking like this: <channel name='#sandbox'>...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
4
by: Gert Van den Eynde | last post by:
Hi all, Could you give me some pointers on how to parse a text input file in C++? Most will be config-file style input (keyword = data), but some maybe 'structures' like material{ name = n,...
3
by: Gregor Horvath | last post by:
Hi, given the dynamic nature of python I assume that there is an elegant solution for my problem, but I did not manage to find it. I have a file that contains for example on line: when...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
6
by: bwaichu | last post by:
I am writing a very basic web server, and I need to parse the HTTP Request string that I am receiving. Are there any good C books that suggest ways to parse strings effectively? Thanks!
0
by: Blacktiger | last post by:
Hi all, I'm new to this list because I had a question about parsing python block structure. I am taking a programming languages course this semester and for our final project we are writing an...
2
by: Andy | last post by:
Hi guys, I'm writing a program with a feature of accepting user input as command text and parsing it to correct function calls...example: "5 minutes later"/"5 min later"/"5 minute...
4
by: =?Utf-8?B?QWxwYW5h?= | last post by:
I am making a thin email client and want to get emails from a pop3 server...Is there any built in support in C# to get emails from a pop3 server and parse the email to show up on the UI ?
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.