minidom questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xtian

    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="ht tp://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:
    [color=blue][color=green][color=darkred]
    >>> s = """<item xmlns=""[/color][/color][/color]
    xmlns:thing="ht tp://www.blah.com">< thing:child
    name="smith"/></item>"""[color=blue][color=green][color=darkred]
    >>> doc = minidom.parseSt ring(s)
    >>> print doc.toxml()[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#2 6>", line 1, in -toplevel-
    print doc.toxml()
    File "C:\PYTHON23\li b\xml\dom\minid om.py", line 47, in toxml
    return self.toprettyxm l("", "", encoding)
    File "C:\PYTHON23\li b\xml\dom\minid om.py", line 59, in toprettyxml
    self.writexml(w riter, "", indent, newl, encoding)
    File "C:\PYTHON23\li b\xml\dom\minid om.py", line 1746, in writexml
    node.writexml(w riter, indent, addindent, newl)
    File "C:\PYTHON23\li b\xml\dom\minid om.py", line 811, in writexml
    _write_data(wri ter, attrs[a_name].value)
    File "C:\PYTHON23\li b\xml\dom\minid om.py", line 301, in _write_data
    data = data.replace("& ", "&amp;").replac e("<", "&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.writexm l 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.writexm l with one that checks to see whether all the
    childNodes are text. Is there a better way to do this?

    Thanks,
    xtian
Working...