XML: Better way to accomplish this?

F

flamesrock

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
 
M

Marc 'BlackJack' Rintsch

flamesrock said:
# <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
 
F

flamesrock

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top