minidom nodes without a document?

Discussion in 'Python' started by Magnus Lie Hetland, Nov 23, 2003.

  1. Is it possible to build a minidom tree bottom-up without access to a
    document node? It seems that simply instantiating the various node
    classes doesn't quite do the trick...

    --
    Magnus Lie Hetland "In this house we obey the laws of
    http://hetland.org thermodynamics!" Homer Simpson
     
    Magnus Lie Hetland, Nov 23, 2003
    #1
    1. Advertising

  2. Magnus Lie Hetland wrote:

    > Is it possible to build a minidom tree bottom-up without access to a
    > document node? It seems that simply instantiating the various node
    > classes doesn't quite do the trick...


    No. In the DOM, all nodes always belong to a Document. They don't have
    to be part of a hiearchy, though - you can create unattached Element
    nodes with createElement[NS], and put them in a hierarchy as you like.

    Regards,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Nov 23, 2003
    #2
    1. Advertising

  3. (Magnus Lie Hetland) wrote in message news:<>...
    > Is it possible to build a minidom tree bottom-up without access to a
    > document node? It seems that simply instantiating the various node
    > classes doesn't quite do the trick...


    I'm not sure what you mean exactly by "without access to...". You need
    a Document instance to create the elements, but they aren't added
    automatically to it.

    >>> import xml.dom.minidom
    >>> doc = xml.dom.minidom.Document()
    >>> foo = doc.createElement('foo')
    >>> for i in range(3):

    .... foo.appendChild(doc.createElement('bar'))
    ....
    >>> doc.toxml()

    '<?xml version="1.0" ?>\n'
    >>> foo.toxml()

    '<foo><bar/><bar/><bar/></foo>'
    >>>


    ----
    levi
     
    Levente Sandor, Nov 24, 2003
    #3
  4. In article <>, Levente
    Sandor wrote:
    > (Magnus Lie Hetland) wrote in message
    >news:<>...
    >> Is it possible to build a minidom tree bottom-up without access to a
    >> document node? It seems that simply instantiating the various node
    >> classes doesn't quite do the trick...

    >
    >I'm not sure what you mean exactly by "without access to...". You need
    >a Document instance to create the elements, but they aren't added
    >automatically to it.
    >

    [snip]

    Right. I understand this. Thanks to you and Martin for mentioning it,
    though :)

    My problem is that I'm building the DOM as part of a bottom-up parse,
    where the various subtrees are build independently from each other,
    through callbacks that are set as part of the parser. So... For all of
    these to have access to a document object (so they can use the
    createFoo methods) I'd have to either (1) hard-code a document into
    the parser (meaning, basically, that each parser instance can only be
    used once), or (2) refactor the parser to supply a context object to
    each callback (which could be, for example, the document object).

    I guess the latter is the way to go, but it does seem awkward that it
    should be necessary. In my case it's OK, as the parsing code is part
    of my project, but if I used an external parser, I'd have to create a
    separate AST and then translate that into a DOM tree afterward; a bit
    unnecessary...

    Oh, well. Can't argue with the DOM, I suppose :)

    --
    Magnus Lie Hetland "In this house we obey the laws of
    http://hetland.org thermodynamics!" Homer Simpson
     
    Magnus Lie Hetland, Nov 24, 2003
    #4
  5. (Magnus Lie Hetland) writes:

    > (1) hard-code a document into
    > the parser (meaning, basically, that each parser instance can only be
    > used once)


    I would do this, and provide a reset() operation.

    > I guess the latter is the way to go, but it does seem awkward that it
    > should be necessary.


    You don't *have* to do that - you could also create stand-alone node,
    providing __init__ parameters as necessary. In that case, you would
    bind yourself to a specific DOM implementation, though - and we do
    reserve the right to change the constructor parameters across releases
    (in fact, we did that in the past).

    Regards,
    Martin
     
    Martin v. =?iso-8859-15?q?L=F6wis?=, Nov 24, 2003
    #5
  6. Magnus Lie Hetland

    John J. Lee Guest

    (Magnus Lie Hetland) writes:

    > In article <>, Levente
    > Sandor wrote:
    > > (Magnus Lie Hetland) wrote in message
    > >news:<>...
    > >> Is it possible to build a minidom tree bottom-up without access to a
    > >> document node? It seems that simply instantiating the various node
    > >> classes doesn't quite do the trick...

    > >
    > >I'm not sure what you mean exactly by "without access to...". You need
    > >a Document instance to create the elements, but they aren't added
    > >automatically to it.
    > >

    > [snip]
    >
    > Right. I understand this. Thanks to you and Martin for mentioning it,
    > though :)
    >
    > My problem is that I'm building the DOM as part of a bottom-up parse,
    > where the various subtrees are build independently from each other,
    > through callbacks that are set as part of the parser. So... For all of

    [...]

    You want DocumentFragment.

    But Martin's right (of course): you still need a Document, and you
    make fragments by calling Document.createDocumentFragment().

    For a headache-inducing example of the use of this (my headache, not
    yours, I hope ;-), see

    http://wwwsearch.sf.net/DOMForm/


    (the parser is in the file DOMForm/_js.py -- it gave me a headache
    because everything prior to a chunk of embedded script in HTML has to
    be available to the script through the document object, so sometimes
    you have to append to a DocumentFragment, sometimes to a node in the
    main Document tree)


    John
     
    John J. Lee, Nov 25, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asd
    Replies:
    3
    Views:
    461
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    548
    gavnosis
    Aug 2, 2003
  3. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    437
    Patrick TJ McPhee
    Jun 17, 2004
  4. Replies:
    3
    Views:
    567
    Stefan Behnel
    Aug 3, 2007
  5. Johannes Bauer
    Replies:
    7
    Views:
    1,096
    Johannes Bauer
    Jun 11, 2009
Loading...

Share This Page