Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > XML: Better way to accomplish this?

Reply
Thread Tools

XML: Better way to accomplish this?

 
 
flamesrock
Guest
Posts: n/a
 
      01-03-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(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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-03-2005
In < .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
 
Reply With Quote
 
 
 
 
flamesrock
Guest
Posts: n/a
 
      01-04-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: A better way to accomplish loop Joseph L. Casale Python 0 02-12-2013 11:01 PM
Re: A better way to accomplish loop Dave Angel Python 0 02-12-2013 08:33 PM
A better way to accomplish loop Joseph L. Casale Python 0 02-12-2013 07:59 PM
Standard way to accomplish production / development switch kellygreer1 ASP .Net Web Services 1 09-27-2006 05:28 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57