XML: Better way to accomplish this?

Discussion in 'Python' started by flamesrock, Jan 3, 2005.

  1. flamesrock

    flamesrock Guest

    Hi,
    I'm working on creating an xml structure like the following, as
    effiecienty and elegantly as possible using minidom preferably:

    #<region>
    # <population>
    # <total>
    # 0
    # </total>
    # <R>
    # 0
    # </R>
    # <C>
    # 0
    # </C>
    # <I>
    # 0
    # </I>
    # </population>
    # <cities>
    # <city1>
    # <cityname/>
    # <mayor/>
    # <morelater/>
    # <citypopulation>
    # <R>
    # 0
    # </R>
    # <C>
    # 0
    # </C>
    # <I>
    # 0
    # </I>
    # </citypopulation>
    # </city1>
    # <city2>
    # <cityname/>
    # <mayor/>
    # <morelater/>
    # <citypopulation>
    # <R>
    # 0
    # </R>
    # <C>
    # 0
    # </C>
    # <I>
    # 0
    # </I>
    # </citypopulation>
    # </city2>
    # <city3 and so on>
    # <cityname/>
    # <mayor/>
    # <morelater/>
    # <citypopulation>
    # <R>
    # 0
    # </R>
    # <C>
    # 0
    # </C>
    # <I>
    # 0
    # </I>
    # </citypopulation>
    # </city3 and so on>
    # </cities>
    #</region>
    (left out)

    The following code accomplishes that, but being a newb to xml..I'm not
    sure if this can be done a better (I'd like to stick with dom due to
    the nature of this app):

    (left textnode parts like mayor, cityname out to keep code concise for
    now)
    # from xml.dom.minidom import parseString
    # #create a new document
    # scoreXML = parseString(u'<region/>'.encode('UTF-8'))
    # art = scoreXML.documentElement
    #
    # #create a total population, cities and some city elements
    # population = scoreXML.createElementNS(None,u'population')
    # cities = scoreXML.createElementNS(None,u'cities')
    # city1 = scoreXML.createElementNS(None,u'city1')
    # city2 = scoreXML.createElementNS(None,u'city2')
    # city3 = scoreXML.createElementNS(None,u'city3 and so on')
    #
    # #add it under the region element
    # art.appendChild(population)
    # art.appendChild(cities)
    #
    # # create a total element with a population number inside
    # # and do this for all RCI numbers
    # population.appendChild(scoreXML.createElementNS(None,u'total'))
    # total = scoreXML.createTextNode(u'0')
    # population.firstChild.appendChild(total)
    # #will get RCI with seperate function
    # RCI = [scoreXML.createTextNode(u'0'),
    # scoreXML.createTextNode(u'0'),
    # scoreXML.createTextNode(u'0')] #[r,c,i]
    # for populationElement in [u'R',u'C',u'I']:
    #
    population.appendChild(scoreXML.createElementNS(None,populationElement))
    # population.lastChild.appendChild(RCI[0])
    # RCI.pop(0)
    #
    # #add the elements underneath city
    # allcities = [city1,city2,city3]
    # for city in allcities:
    # cities.appendChild(city)
    #
    # for cityattribute in [u'cityname',u'mayor',u'morelater']:
    # city.appendChild(scoreXML.createElementNS(None,cityattribute))
    #
    # citypopulation = scoreXML.createElementNS(None,u'citypopulation')
    # city.appendChild(citypopulation)
    # #will get RCI with seperate function
    # RCI = [scoreXML.createTextNode(u'0'),
    # scoreXML.createTextNode(u'0'),
    # scoreXML.createTextNode(u'0')] #[r,c,i]
    #
    # for populationElement in [u'R',u'C',u'I']:
    #>>>>>>>>>>citypopulation.appendChild(scoreXML.createElementNS(None,populationElement))
    # citypopulation.lastChild.appendChild(RCI[0])
    # RCI.pop(0)
    # #write the result
    # print scoreXML.toprettyxml()


    Any ideas?

    -thanks in advance
     
    flamesrock, Jan 3, 2005
    #1
    1. Advertising

  2. In <>, flamesrock
    wrote:

    > # <cities>
    > # <city1>
    > [...]
    > # </city1>
    > # <city2>
    > [...]
    > # </city2>
    > # <city3 and so on>
    > [...]
    > # </city3 and so on>
    > # </cities>


    Don't you think it's better to use an attribute for the city nr.?
    Something like ``<city nr="1">``. At least if you intent to write a DTD
    or Schema this might be better.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jan 3, 2005
    #2
    1. Advertising

  3. flamesrock

    flamesrock Guest

    Good idea! Thanks

    Also, besides the document structure (I appreciate comments on that
    too, of course,) do you think the code is efficient for xml? Any
    special tricks you know of?

    -thanks
     
    flamesrock, Jan 4, 2005
    #3
    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. kellygreer1

    Standard way to accomplish production / development switch

    kellygreer1, Sep 27, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    122
    bruce barker \(sqlwork.com\)
    Sep 27, 2006
  2. Joseph L. Casale

    A better way to accomplish loop

    Joseph L. Casale, Feb 12, 2013, in forum: Python
    Replies:
    0
    Views:
    123
    Joseph L. Casale
    Feb 12, 2013
  3. Dave Angel

    Re: A better way to accomplish loop

    Dave Angel, Feb 12, 2013, in forum: Python
    Replies:
    0
    Views:
    156
    Dave Angel
    Feb 12, 2013
  4. Joseph L. Casale

    RE: A better way to accomplish loop

    Joseph L. Casale, Feb 12, 2013, in forum: Python
    Replies:
    0
    Views:
    126
    Joseph L. Casale
    Feb 12, 2013
  5. Replies:
    2
    Views:
    72
    Mark H Harris
    May 13, 2014
Loading...

Share This Page