473,387 Members | 1,834 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,387 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"/>
</item>

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?

Thanks,
xtian
Jul 18 '05 #1
0 7465

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

Similar topics

0
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...
5
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...
4
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...
6
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...
1
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 =...
18
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...
0
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...
3
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.