By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

XML: Better way to accomplish this?

P: n/a
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(No ne,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(No ne,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,cit yattribute))
#
# 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.cre ateElementNS(None,populationElement))
# citypopulation.lastChild.appendChild(RCI[0])
# RCI.pop(0)
# #write the result
# print scoreXML.toprettyxml()
Any ideas?

-thanks in advance

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In <11**********************@c13g2000cwb.googlegroups .com>, 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
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.