XML: Better way to accomplish this?

flamesrock flamesrock at gmail.com
Sun Jan 2 23:54:20 EST 2005


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




More information about the Python-list mailing list