Create an XML document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kyosohma@gmail.com

    #1

    Create an XML document

    Hi all,

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

    <zAppointment s reminder="15">
    <appointment>
    <begin>11797758 00</begin>
    <duration>180 0</duration>
    </appointment>
    </zAppointments>

    I tried using minidom with the following code:

    <code>

    from xml.dom.minidom import Document

    doc = Document()

    zappt = doc.createEleme nt('zAppointmen ts')
    zappt.setAttrib ute('reminder', '15')
    doc.appendChild (zappt)

    appt = doc.createEleme nt('appointment ')
    zappt.appendChi ld(appt)

    begin = doc.createEleme nt('begin')
    appt.appendChil d(begin)
    f = file(r'path\to\ file.xml', 'w')
    f.write(doc.top rettyxml(indent =' '))
    f.close()

    </code>


    This gives me the following:

    <?xml version="1.0" ?>
    <zAppointment s 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

  • =?ISO-8859-1?Q?Nis_J=F8rgensen?=

    #2
    Re: Create an XML document

    kyosohma@gmail. com skrev:
    Hi all,
    >
    I am attempting to create an XML document dynamically with Python. It
    needs the following format:
    >
    <zAppointment s reminder="15">
    <appointment>
    <begin>11797758 00</begin>
    <duration>180 0</duration>
    </appointment>
    </zAppointments>
    >
    I tried using minidom with the following code:
    >
    <code>
    >
    from xml.dom.minidom import Document
    >
    doc = Document()
    >
    zappt = doc.createEleme nt('zAppointmen ts')
    zappt.setAttrib ute('reminder', '15')
    doc.appendChild (zappt)
    >
    appt = doc.createEleme nt('appointment ')
    zappt.appendChi ld(appt)
    >
    begin = doc.createEleme nt('begin')
    appt.appendChil d(begin)
    f = file(r'path\to\ file.xml', 'w')
    f.write(doc.top rettyxml(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.createEleme nt("foo") # creates <foo />
    txt = dom.createTextN ode("hello, world!") # creates "hello, world!"
    x.appendChild(t xt) # results in <foo>hello, world!</foo>
    dom.childNodes[1].appendChild(x) # appends at end of 1st child's \ children
    print dom.toxml()

    Comment

    • Matimus

      #3
      Re: Create an XML document

      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.createText Node' instead of 'doc.createElem ent' to create it.


      <code>

      from xml.dom.minidom import Document

      doc = Document()

      zappt = doc.createEleme nt('zAppointmen ts')
      zappt.setAttrib ute('reminder', '15')
      doc.appendChild (zappt)

      appt = doc.createEleme nt('appointment ')
      zappt.appendChi ld(appt)

      begin = doc.createEleme nt('begin')
      begincont = doc.createTextE lement('1179775 800')
      begin.appendChi ld(begincont)
      appt.appendChil d(begin)

      duration = doc.createEleme nt('duration')
      durationcont = doc.createTextE lement('1800')
      duration.append Child(durationc ont)
      appt.appendChil d(duration)

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

      </code>

      Comment

      • kyosohma@gmail.com

        #4
        Re: Create an XML document

        On May 22, 10:00 am, kyoso...@gmail. com wrote:
        Hi all,
        >
        I am attempting to create an XML document dynamically with Python. It
        needs the following format:
        >
        <zAppointment s reminder="15">
        <appointment>
        <begin>11797758 00</begin>
        <duration>180 0</duration>
        </appointment>
        </zAppointments>
        >
        I tried using minidom with the following code:
        >
        <code>
        >
        from xml.dom.minidom import Document
        >
        doc = Document()
        >
        zappt = doc.createEleme nt('zAppointmen ts')
        zappt.setAttrib ute('reminder', '15')
        doc.appendChild (zappt)
        >
        appt = doc.createEleme nt('appointment ')
        zappt.appendChi ld(appt)
        >
        begin = doc.createEleme nt('begin')
        appt.appendChil d(begin)
        f = file(r'path\to\ file.xml', 'w')
        f.write(doc.top rettyxml(indent =' '))
        f.close()
        >
        </code>
        >
        This gives me the following:
        >
        <?xml version="1.0" ?>
        <zAppointment s 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.createText Element". I think I know what you mean though.

        Thank you both for your advice. It works now.

        Mike

        Comment

        • Stefan Behnel

          #5
          Re: Create an XML document

          kyosohma@gmail. com wrote:
          I am attempting to create an XML document dynamically with Python. It
          needs the following format:
          >
          <zAppointment s reminder="15">
          <appointment>
          <begin>11797758 00</begin>
          <duration>180 0</duration>
          </appointment>
          </zAppointments>
          Try lxml.objectify.


          >>from lxml import etree, objectify
          >>zAppointmen ts = objectify.Eleme nt("zAppointmen ts")
          >>zAppointments .set("reminder" , "15")
          >>zAppointments .appointment = objectify.Eleme nt("appointment ")
          >>zAppointments .appointment.be gin = 1179775800
          >>zAppointments .appointment.du ration = 1800
          >>print etree.tostring( zAppointments, pretty_print=Tr ue)
          <zAppointment s reminder="15">
          <appointment>
          <begin>11797758 00</begin>
          <duration>180 0</duration>
          </appointment>
          </zAppointments>

          Pretty much what one would expect.

          Stefan

          Comment

          • kyosohma@gmail.com

            #6
            Re: Create an XML document

            On May 22, 7:30 pm, Stefan Behnel <stefan.behne l-n05...@web.dewr ote:
            kyoso...@gmail. com wrote:
            I am attempting to create an XML document dynamically with Python. It
            needs the following format:
            >
            <zAppointment s reminder="15">
            <appointment>
            <begin>11797758 00</begin>
            <duration>180 0</duration>
            </appointment>
            </zAppointments>
            >
            Try lxml.objectify.
            >

            >
            >>from lxml import etree, objectify
            >>zAppointmen ts = objectify.Eleme nt("zAppointmen ts")
            >>zAppointments .set("reminder" , "15")
            >>zAppointments .appointment = objectify.Eleme nt("appointment ")
            >>zAppointments .appointment.be gin = 1179775800
            >>zAppointments .appointment.du ration = 1800
            >
            >>print etree.tostring( zAppointments, pretty_print=Tr ue)
            <zAppointment s reminder="15">
            <appointment>
            <begin>11797758 00</begin>
            <duration>180 0</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

            Comment

            Working...