Create an XML document

Discussion in 'Python' started by kyosohma@gmail.com, May 22, 2007.

  1. Guest

    Hi all,

    I am attempting to create an XML document dynamically with Python. It
    needs the following format:

    <zAppointments reminder="15">
    <appointment>
    <begin>1179775800</begin>
    <duration>1800</duration>
    </appointment>
    </zAppointments>

    I tried using minidom with the following code:

    <code>

    from xml.dom.minidom import Document

    doc = Document()

    zappt = doc.createElement('zAppointments')
    zappt.setAttribute('reminder', '15')
    doc.appendChild(zappt)

    appt = doc.createElement('appointment')
    zappt.appendChild(appt)

    begin = doc.createElement('begin')
    appt.appendChild(begin)
    f = file(r'path\to\file.xml', 'w')
    f.write(doc.toprettyxml(indent=' '))
    f.close()

    </code>


    This gives me the following:

    <?xml version="1.0" ?>
    <zAppointments reminder="15">
    <appointment>
    <begin/>
    <duration/>
    </appointment>
    </zAppointments>

    How do I get Python to put values into the elements by tag name? I can
    parse my documents just fine, but now I need to edit them and write
    them out to a file for an application I am working on. I am sure I am
    missing something obvious.

    Thanks a lot!

    Mike
     
    , May 22, 2007
    #1
    1. Advertising

  2. skrev:
    > Hi all,
    >
    > I am attempting to create an XML document dynamically with Python. It
    > needs the following format:
    >
    > <zAppointments reminder="15">
    > <appointment>
    > <begin>1179775800</begin>
    > <duration>1800</duration>
    > </appointment>
    > </zAppointments>
    >
    > I tried using minidom with the following code:
    >
    > <code>
    >
    > from xml.dom.minidom import Document
    >
    > doc = Document()
    >
    > zappt = doc.createElement('zAppointments')
    > zappt.setAttribute('reminder', '15')
    > doc.appendChild(zappt)
    >
    > appt = doc.createElement('appointment')
    > zappt.appendChild(appt)
    >
    > begin = doc.createElement('begin')
    > appt.appendChild(begin)
    > f = file(r'path\to\file.xml', 'w')
    > f.write(doc.toprettyxml(indent=' '))
    > f.close()
    >
    > </code>


    > How do I get Python to put values into the elements by tag name? I can
    > parse my documents just fine, but now I need to edit them and write
    > them out to a file for an application I am working on. I am sure I am
    > missing something obvious.


    From http://wiki.python.org/moin/MiniDom

    Add an Element with Text Inside

    Create & add an XML element to an XML document, the element has text inside.

    ex: <foo>hello, world!</foo>


    from xml.dom.minidom import parse

    dom = parse("bar.xml")
    x = dom.createElement("foo") # creates <foo />
    txt = dom.createTextNode("hello, world!") # creates "hello, world!"
    x.appendChild(txt) # results in <foo>hello, world!</foo>
    dom.childNodes[1].appendChild(x) # appends at end of 1st child's \ children
    print dom.toxml()
     
    =?ISO-8859-1?Q?Nis_J=F8rgensen?=, May 22, 2007
    #2
    1. Advertising

  3. Matimus Guest

    > How do I get Python to put values into the elements by tag name?

    The same way you create put in new elements. Only, you use
    'doc.createTextNode' instead of 'doc.createElement' to create it.


    <code>

    from xml.dom.minidom import Document

    doc = Document()

    zappt = doc.createElement('zAppointments')
    zappt.setAttribute('reminder', '15')
    doc.appendChild(zappt)

    appt = doc.createElement('appointment')
    zappt.appendChild(appt)

    begin = doc.createElement('begin')
    begincont = doc.createTextElement('1179775800')
    begin.appendChild(begincont)
    appt.appendChild(begin)

    duration = doc.createElement('duration')
    durationcont = doc.createTextElement('1800')
    duration.appendChild(durationcont)
    appt.appendChild(duration)

    f = file(r'path\to\file.xml', 'w')
    f.write(doc.toprettyxml(indent=' '))
    f.close()

    </code>
     
    Matimus, May 22, 2007
    #3
  4. Guest

    On May 22, 10:00 am, wrote:
    > Hi all,
    >
    > I am attempting to create an XML document dynamically with Python. It
    > needs the following format:
    >
    > <zAppointments reminder="15">
    > <appointment>
    > <begin>1179775800</begin>
    > <duration>1800</duration>
    > </appointment>
    > </zAppointments>
    >
    > I tried using minidom with the following code:
    >
    > <code>
    >
    > from xml.dom.minidom import Document
    >
    > doc = Document()
    >
    > zappt = doc.createElement('zAppointments')
    > zappt.setAttribute('reminder', '15')
    > doc.appendChild(zappt)
    >
    > appt = doc.createElement('appointment')
    > zappt.appendChild(appt)
    >
    > begin = doc.createElement('begin')
    > appt.appendChild(begin)
    > f = file(r'path\to\file.xml', 'w')
    > f.write(doc.toprettyxml(indent=' '))
    > f.close()
    >
    > </code>
    >
    > This gives me the following:
    >
    > <?xml version="1.0" ?>
    > <zAppointments reminder="15">
    > <appointment>
    > <begin/>
    > <duration/>
    > </appointment>
    > </zAppointments>
    >
    > How do I get Python to put values into the elements by tag name? I can
    > parse my documents just fine, but now I need to edit them and write
    > them out to a file for an application I am working on. I am sure I am
    > missing something obvious.
    >
    > Thanks a lot!
    >
    > Mike


    Thanks Nis. Your code worked great. For some reason, Google Groups
    seems hosed up today and makes seeing replies impossible on some
    posts.

    Maximus - What the? You told me to use "createTextNode" and then you
    used "doc.createTextElement". I think I know what you mean though.

    Thank you both for your advice. It works now.

    Mike
     
    , May 22, 2007
    #4
  5. wrote:
    > I am attempting to create an XML document dynamically with Python. It
    > needs the following format:
    >
    > <zAppointments reminder="15">
    > <appointment>
    > <begin>1179775800</begin>
    > <duration>1800</duration>
    > </appointment>
    > </zAppointments>


    Try lxml.objectify.

    http://codespeak.net/lxml/dev/objectify.html

    >>> from lxml import etree, objectify
    >>> zAppointments = objectify.Element("zAppointments")
    >>> zAppointments.set("reminder", "15")
    >>> zAppointments.appointment = objectify.Element("appointment")
    >>> zAppointments.appointment.begin = 1179775800
    >>> zAppointments.appointment.duration = 1800


    >>> print etree.tostring(zAppointments, pretty_print=True)

    <zAppointments reminder="15">
    <appointment>
    <begin>1179775800</begin>
    <duration>1800</duration>
    </appointment>
    </zAppointments>

    Pretty much what one would expect.

    Stefan
     
    Stefan Behnel, May 23, 2007
    #5
  6. Guest

    On May 22, 7:30 pm, Stefan Behnel <> wrote:
    > wrote:
    > > I am attempting to create an XML document dynamically with Python. It
    > > needs the following format:

    >
    > > <zAppointments reminder="15">
    > > <appointment>
    > > <begin>1179775800</begin>
    > > <duration>1800</duration>
    > > </appointment>
    > > </zAppointments>

    >
    > Try lxml.objectify.
    >
    > http://codespeak.net/lxml/dev/objectify.html
    >
    > >>> from lxml import etree, objectify
    > >>> zAppointments = objectify.Element("zAppointments")
    > >>> zAppointments.set("reminder", "15")
    > >>> zAppointments.appointment = objectify.Element("appointment")
    > >>> zAppointments.appointment.begin = 1179775800
    > >>> zAppointments.appointment.duration = 1800

    >
    > >>> print etree.tostring(zAppointments, pretty_print=True)

    > <zAppointments reminder="15">
    > <appointment>
    > <begin>1179775800</begin>
    > <duration>1800</duration>
    > </appointment>
    > </zAppointments>
    >
    > Pretty much what one would expect.
    >
    > Stefan


    Stefan,

    This looks really cool. I'll try implementing it sometime today and
    see if it affects my execution time any. It's definitely clearer
    code...at least in my opinion.

    Mike
     
    , May 23, 2007
    #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. Tony Prichard
    Replies:
    0
    Views:
    787
    Tony Prichard
    Dec 12, 2003
  2. Manish Hatwalne
    Replies:
    1
    Views:
    421
    Martin Honnen
    Jul 13, 2004
  3. Replies:
    4
    Views:
    712
  4. Krithika San
    Replies:
    3
    Views:
    355
    Marnen Laibow-Koser
    Nov 14, 2009
  5. icogs
    Replies:
    0
    Views:
    154
    icogs
    Jan 31, 2008
Loading...

Share This Page