minidom: find what file the element was created in?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweavo
    New Member
    • Mar 2008
    • 1

    minidom: find what file the element was created in?

    Hi all,

    (Python 2.5 under cygwin)

    I'm reading a bunch of XML files and merging them in memory - each file contains a number of packages which are to be merged:

    Code:
    for fname in gInfiles:
        doc=xml.dom.minidom.parse(fname)
        if gDoc is None:
            gDoc=doc
            gTlp=relPathQuery(gDoc,'TOP-LEVEL-PACKAGES')[0] # There shall be only one
        for nPackage in relPathQuery(doc,'TOP-LEVEL-PACKAGES/PACKAGE'):
            info("  - " + relPathQueryContent(nPackage,'SHORT-NAME','(unknown)').strip())
            if doc!=gDoc:
                gTlp.appendChild(nPackage.cloneNode(1))
    There is a lot of validation besides XML validation, which my python does, by querying the tree, and checking that the input makes sense. If it does not make sense, or if I want to emit warnings, I would like to give the user meaningful pointers into the source files. That is, I want

    Code:
    ERROR: %{problem}s at %{filename}s, line %{lineno}d.
    I've crawled around the minidom and DOM python documentation but haven't seen anything that might help. I also started to look in the source of minidom.py and expatbuilder.py but it's getting rather time consuming when someone might just know this OTOH!

    Anyone done this? Ta

    --
    Steve@phoenix ~/
    $ python -V
    Python 2.5.1

    Steve@phoenix ~/
    $ python -v
    # installing zipimport hook
    import zipimport # builtin
    # installed zipimport hook
    # /usr/lib/python2.5/site.pyc matches /usr/lib/python2.5/site.py
    import site # precompiled from /usr/lib/python2.5/site.pyc
    # /usr/lib/python2.5/os.pyc matches /usr/lib/python2.5/os.py
    import os # precompiled from /usr/lib/python2.5/os.pyc
    import posix # builtin
    # /usr/lib/python2.5/posixpath.pyc matches /usr/lib/python2.5/posixpath.py
    import posixpath # precompiled from /usr/lib/python2.5/posixpath.pyc
    # /usr/lib/python2.5/stat.pyc matches /usr/lib/python2.5/stat.py
    import stat # precompiled from /usr/lib/python2.5/stat.pyc
    # /usr/lib/python2.5/UserDict.pyc matches /usr/lib/python2.5/UserDict.py
    import UserDict # precompiled from /usr/lib/python2.5/UserDict.pyc
    # /usr/lib/python2.5/copy_reg.pyc matches /usr/lib/python2.5/copy_reg.py
    import copy_reg # precompiled from /usr/lib/python2.5/copy_reg.pyc
    # /usr/lib/python2.5/types.pyc matches /usr/lib/python2.5/types.py
    import types # precompiled from /usr/lib/python2.5/types.pyc
    import _types # builtin
    # /usr/lib/python2.5/warnings.pyc matches /usr/lib/python2.5/warnings.py
    import warnings # precompiled from /usr/lib/python2.5/warnings.pyc
    # /usr/lib/python2.5/linecache.pyc matches /usr/lib/python2.5/linecache.py
    import linecache # precompiled from /usr/lib/python2.5/linecache.pyc
    import encodings # directory /usr/lib/python2.5/encodings
    # /usr/lib/python2.5/encodings/__init__.pyc matches /usr/lib/python2.5/encodings/__init__.py
    import encodings # precompiled from /usr/lib/python2.5/encodings/__init__.pyc
    # /usr/lib/python2.5/codecs.pyc matches /usr/lib/python2.5/codecs.py
    import codecs # precompiled from /usr/lib/python2.5/codecs.pyc
    import _codecs # builtin
    # /usr/lib/python2.5/encodings/aliases.pyc matches /usr/lib/python2.5/encodings/aliases.py
    import encodings.alias es # precompiled from /usr/lib/python2.5/encodings/aliases.pyc
    # /usr/lib/python2.5/encodings/ascii.pyc matches /usr/lib/python2.5/encodings/ascii.py
    import encodings.ascii # precompiled from /usr/lib/python2.5/encodings/ascii.pyc
    Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
    [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
    Type "help", "copyright" , "credits" or "license" for more information.
    dlopen("/usr/lib/python2.5/lib-dynload/readline.dll", 2);
    import readline # dynamically loaded from /usr/lib/python2.5/lib-dynload/readline.dll
    >>>
Working...