|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Confusion with GetAttribute/GetElementsBytagName
Hi all,
I'm a little confused and was wondering whether someone could help explain something to me. My questions are located to the right of the tags preceeded by a * and at the end of the XML example. Here is a simple XML file: <Countries> *This is the document root isn't it? <Argentina> *What is this then? the root as well?? <Pop>1000000</Pop> <Area>2000000sqkm<Area> </Argentina> <Bolivia> ...etc... </Bolivia> </Countries> I have an SVG document (map of the world) which I want to access an external XML file (containing data like the stuff)above). Now, when the user clicks on the SVG map, I want the data associated with that click to be matched in the XML file and returned to the browser. So...in the SVG document, I have an id field which contains the countries name in it. I will then extract this when onclick is activated and then use GetElementsByTagName to get the correct "row" of XML data, then use GetAttribute to select the child nodes underneath. Now, that is my understanding of what I need to do...but is it right and more importantly efficient??? I keep getting confused with the document root and the childs underneath since virtually all examples of XML documents illustrate this: <ListCountries> *This is the document root isn't it? <Country> *What is this then? the root as well?? <Pop>1000000</Pop> <Area>2000000sqkm<Area> </Country> <Country> ...etc... </Country> </ListCountries> --> in the above example...how on earth am I to use the GetElementsByTagName to get the appropriate "row" of data since they're all called Country??? Is it acceptable to have an XML file such as the one at the top of the list?? Thanx Ben |
|
#2
|
||||
|
||||
|
In your first example the countries element is the root and one has to have a root element in a xml-file. Argentina on the other hand is not a root element rather than just a normal xml element.
On the second example the listcountries is the root once again and the country element are just normal xml elements. And as an answer to your effiency question. Since you are using the GetElementsByTagName the first one is probably more efficient. The latter one can be done with GetElementsByTagName but you need to add some attribute information to the tag for finding the correct one e.g. <country name='finland'> I suggest using the first one if you use getElementsByTagName... -Miska- |
|
#3
|
|||
|
|||
|
Ah, okay, so in the 2nd example. I would have:
<Country> ... </Country> <Country> ... </Country> and then add name="whatever" to each <Country> element and then use GetAttribute to access the attribute element of Country to get all the child nodes?? So thereby skipping on the GetElementByTagName command since all the elements will have the same name (eg: Country?) Which is faster or more acceptable in your view? Thank you!! Ben |
|
#4
|
||||
|
||||
|
I would do it like the first one, but it all once again depends one the programming interface that you are using (I'm a PHP menace). If you are using DOM or something else (SAX, etc.). You're probably using the DOM looking from your description.
The latter on is better if you have a function like getElementByAttributeValue, though this function can be easily made to almost any programming language. -Miska- |
|
#5
|
|||
|
|||
|
Miska, Okay, but how do you specify a DTD header for a document?? The same way as is already? If I have different elements under the root (1st example) then it'll be a massive DTD instead of the 2nd one which is easier.
|
|
#6
|
||||
|
||||
|
Quote:
That's true, but if you use XML Schema instead, then the first one is easily done as well... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Confusion with GetAttribute/GetElementsBytagName |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|