473,230 Members | 1,464 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,230 software developers and data experts.

minidom questions

Hi -

I'm doing some data conversion with minidom (turning a csv file into a
specific xml format), and I've hit a couple of small problems.

1: The output format has a header with some xml that looks something
like this:
<item xmlns="" xmlns:thing="http://www.blah.com">
<thing:child name="smith"/>

As I understand it, this is a valid use of namespaces.
If I add this to the start of the document, when I do a .toxml(), I
get an exception. Here's a small example:
s = """<item xmlns="" xmlns:thing="http://www.blah.com"><thing:child
name="smith"/></item>""" doc = minidom.parseString(s)
print doc.toxml()

Traceback (most recent call last):
File "<pyshell#26>", line 1, in -toplevel-
print doc.toxml()
File "C:\PYTHON23\lib\xml\dom\minidom.py", line 47, in toxml
return self.toprettyxml("", "", encoding)
File "C:\PYTHON23\lib\xml\dom\minidom.py", line 59, in toprettyxml
self.writexml(writer, "", indent, newl, encoding)
File "C:\PYTHON23\lib\xml\dom\minidom.py", line 1746, in writexml
node.writexml(writer, indent, addindent, newl)
File "C:\PYTHON23\lib\xml\dom\minidom.py", line 811, in writexml
_write_data(writer, attrs[a_name].value)
File "C:\PYTHON23\lib\xml\dom\minidom.py", line 301, in _write_data
data = data.replace("&", "&amp;").replace("<", "&lt;")
AttributeError: 'NoneType' object has no attribute 'replace'

Doing some debugging, the xmlns attribute (is it really an attribute?)
has a value of None, rather than "".
I can work around this by replacing the implementation of
Element.writexml with one including:

value = attrs[a_name].value
if value is None:
value = ""

Is this a bug? Am I doing something wrong?

2: Formatting - I'd like the output xml not to put extra line breaks
inside elements that contain only text nodes (which is what
..toprettyxml does by default) - the tool that uses the xml treats the
line breaks as significant. The .toxml method works, but I'd like to
have the output be prettier than this (while not being as pretty as
the output of .toprettyxml :). I can see how to get what I want by
replacing Element.writexml with one that checks to see whether all the
childNodes are text. Is there a better way to do this?

Jul 18 '05 #1
0 7442

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

Similar topics

by: haughki | last post by:
apologies for asking this. it must have been answered before. i have really tried to find the answer elsewhere. a simple question: i'd like to remove all ignorable whitespace from my minidom...
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
by: Dan | last post by:
I'm using python's xml.dom.minidom module to generate xml files, and I'm running into memory problems. The xml files I'm trying to create are relatively flat, with one root node which may have...
by: Maksim Kasimov | last post by:
Hi, i'm faced with such a problem when i use xml.dom.minidom: to append all child nodes from "doc" in "_requ" to "doc" in "_resp", i do the following: _requ =...
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml version="1.0"?>\n<Data><!]></Data>\n' #After i...
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an error on XP if there is an empty namespace. The...
by: aine_canby | last post by:
Hi, I'm working with a number of scripts which were written years ago for my company for Python 2.2, and I'd like to update for Python 2.5. I have written a script to add # -*- coding: cp1252...
by: ashmir.d | last post by:
Hi, I am trying to parse an xml file using the minidom parser. <code> from xml.dom import minidom xmlfilename = "sample.xml" xmldoc = minidom.parse(xmlfilename) </code> The parser is...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.