473,503 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML: Better way to accomplish this?

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
2 1264
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2093
by: Gene Ellis | last post by:
Our sys op won't/can't install the server side XSLT libraries, to allow us perform server-side translations on our webserver It is a Linux machine. Is it possible to put the XML and XSL files on a...
3
2042
by: Tim Dempsey | last post by:
Folks, I need some advice. I hpoe some of you can advise me. Our church's weekly bulletin has been published on paper for years. It is created in MS Word and sent to the publishing company....
1
1429
by: milkyway | last post by:
Hello, I am considering the following in order to accomplish my task. An application has to be created that runs from a regular computer as well as from a WAP-enabled device. When the user is...
8
1245
by: Wayne Wengert | last post by:
I am trying to build a VB.NET Windows application in which I want to create an XML file from data collected from the user and stored in arrays. I am looking for any pointers to information on how...
6
13468
by: Thomas Polan | last post by:
Sorry if this has been posted before... I am receiving XML messages over a TCP client. Messages vary in size and sometimes can arrive in groups. Thus, I am not guaranteed to receive a full...
3
1807
by: Kanchana D S | last post by:
Hi, I am trying to design the schema for an XML file. The approach that I am planning to follow is to have 2 XSD files, each containing some relevant information about the elements in the...
4
3580
by: Scott | last post by:
I think I have confused myself with reading all the articles on XML so I am hoping someone can point me in the direction for each piece of the project I am trying to develop. 1st Step - I need...
0
1244
by: wyattroerb | last post by:
Hi, like a lot of other newbies in xml i suffer from the various ways you can take for xml data processing in python. Here are my two major problems: Problem number one: A application written...
2
2016
by: -D- | last post by:
I'm taking my first stab at using xml, so please bear with my novice questions and understanding of xml. I'm trying to create an xml file that holds all my website navigation. If I understand...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.