XML root node attributes

Discussion in 'Python' started by Slafs, Nov 17, 2009.

  1. Slafs

    Slafs Guest

    Hi

    I'm little confused about adding attributes to the root node when
    creating an XML document.
    Can I do this using minidom or something else.
    I can't find anything that would fit my needs.

    i would like to have something like this:
    <?xml ... ?>
    <root a="v" b="v2" c="v3">
    <d ... > </d>
    ....
    </root>

    Please help.

    Regards.
    Slafs, Nov 17, 2009
    #1
    1. Advertising

  2. Slafs, 17.11.2009 15:19:
    > I'm little confused about adding attributes to the root node when
    > creating an XML document.
    > Can I do this using minidom or something else.


    Yes, you /can/, but you /should/ use something else.


    > I can't find anything that would fit my needs.
    >
    > i would like to have something like this:
    > <?xml ... ?>
    > <root a="v" b="v2" c="v3">
    > <d ... > </d>
    > ....
    > </root>


    Use ElementTree:

    import xml.etree.ElementTree as ET
    root = ET.Element("root", dict(a='v', b='v2', c='v3'))
    root.SubElement('d')

    print ET.tostring(root)

    Stefan
    Stefan Behnel, Nov 17, 2009
    #2
    1. Advertising

  3. -----------------------------------
    The information contained in this electronic message and any attached document(s) is intended only for the personal and confidential use of the designated recipients named above. This message may be confidential. If the reader of this message is not the intended recipient, you are hereby notified that you have received this document in error, and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify sender immediately by telephone (603) 262-6300 or by electronic mail immediately. Thank you.

    -----Original Message-----
    From: python-list-bounces+mmitchell=
    [mailto:python-list-bounces+mmitchell=] On
    Behalf Of Slafs
    Sent: Tuesday, November 17, 2009 9:20 AM
    To:
    Subject: XML root node attributes

    Hi

    I'm little confused about adding attributes to the root node when
    creating an XML document.
    Can I do this using minidom or something else.
    I can't find anything that would fit my needs.

    i would like to have something like this:
    <?xml ... ?>
    <root a="v" b="v2" c="v3">
    <d ... > </d>
    ....
    </root>

    Please help.

    Regards.
    --
    http://mail.python.org/mailman/listinfo/python-list


    Hi,

    I'm sure someone will point out a better way to do it but yes, you can
    do it with minidom.

    from xml.dom.minidom import Document

    doc = Document()

    root = doc.createElement('root')
    root.setAttribute('a', 'v')
    root.setAttribute('b', 'v2')
    root.setAttribute('c', '3')
    doc.appendChild(root)

    d = doc.createElement('d')
    root.appendChild(d)

    print doc.toprettyxml()
    Matt Mitchell, Nov 17, 2009
    #3
  4. Slafs

    Slafs Guest

    Thanks

    But this doesn't work. I've ended using something like this:

    import xml.etree.ElementTree as ET
    root = ET.Element("root", dict(a='v', b='v2', c='v3'))
    n = ET.SubElement(root,'d')
    tree = ET.ElementTree(root)
    import sys
    tree.write(sys.stdout)


    On 17 Lis, 15:36, Stefan Behnel <> wrote:
    > Slafs, 17.11.2009 15:19:
    >
    > > I'm little confused about adding attributes to the root node when
    > > creating an XML document.
    > > Can I do this using minidom or something else.

    >
    > Yes, you /can/, but you /should/ use something else.
    >
    > > I can't find anything that would fit my needs.

    >
    > > i would like to have something like this:
    > > <?xml ... ?>
    > > <root a="v" b="v2" c="v3">
    > >     <d ... > </d>
    > >    ....
    > > </root>

    >
    > Use ElementTree:
    >
    >     import xml.etree.ElementTree as ET
    >     root = ET.Element("root", dict(a='v', b='v2', c='v3'))
    >     root.SubElement('d')
    >
    >     print ET.tostring(root)
    >
    > Stefan
    Slafs, Nov 18, 2009
    #4
    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. Replies:
    0
    Views:
    1,448
  2. Replies:
    2
    Views:
    1,863
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    1
    Views:
    563
    Martin Honnen
    May 8, 2007
  5. Rodolphe G
    Replies:
    4
    Views:
    631
    Rodolphe G
    Jun 1, 2007
Loading...

Share This Page