473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Extracting XML Tag Names

Hello, I need to get the tag name from an XML file. I know where it is going to be everytime, but I do not know what the name will be. This is a snippet of my xml data.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <Forecast>
  3.  <Config/>
  4.  <Date>THU SEP 9 2010</Date>
  5.  <Time>4:53 PM CDT</Time>
  6. <Thursday>
  7.  <Conditions>Partly Cloudy</Conditions>
  8.  <Description>Partly cloudy</Description>
  9.  <Image>http://www.</Image>
  10.  <High>68°F</High>
  11.  <Low>50°F</Low>
  12.  <Pop>0</Pop>
  13.  </Thursday> 
  14.  
I need to extract 'Thursday' from the tag to know what day the weather data is for. Tomorrow it will be Friday and so on and so forth. It will always be in the same place. I have tried this,

Expand|Select|Wrap|Line Numbers
  1.  
  2. forecast_node = doc.getElementsByTagName('Forecast')
  3. current_day = forecast_node[0].childNodes[4].tagName 
but it seems that I cannot use tagName in this manner. Any suggestions for me?
Sep 9 '10 #1
2 1726
bvdet
2,851 Expert Mod 2GB
It seems that you want the fourth child element node of the Forecast node. To get that, you must skip the text nodes. Example:
Expand|Select|Wrap|Line Numbers
  1. xmlDoc = minidom.parseString(xmlStr)
  2. forecast_node = xmlDoc.getElementsByTagName('Forecast')
  3.  
  4. # get 4th element node
  5. count = 0
  6. for child in forecast_node[0].childNodes:
  7.     if child.nodeType == 1:
  8.         count += 1
  9.         if count == 4:
  10.             break
  11. print child
Sep 10 '10 #2
Thanks for pointing me this way, I am very new to this and never would have thought of it. Had to to change to
Expand|Select|Wrap|Line Numbers
  1. print child.toxml
to get the tag name completely out of the dom element. Thanks again!
Sep 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
7
by: dixie | last post by:
I have a text box on a form. In that text box is a list of people separated by a comma. Like A.Smith, L.Jones, P.Somebody, H. Somebodyelse Note, there is no full stop at the end of the list...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
2
by: Gary Wessle | last post by:
Hi I would appreciate any idea on how to approach this task. I have files which look like this: file1 men 234 women 112 children 4 cars 11
2
by: John | last post by:
Hi How can I extract the first name from the full name where each name is separated by spaces? Thanks Regards
6
by: rlntemp-gng | last post by:
I need to extract information from some Excel files but am stuck with part of it: As an example, I have the following Excel File that has 3 tabbed sheets: FileName: ...
4
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
4
by: Charlie Hoffpauir | last post by:
How do I extract a surname from a name field in which the name is in the form: <surname, given names>. My first though was to run an update query, but I can't find the function that will give me...
4
tharden3
by: tharden3 | last post by:
At work I have to spend all day punching in database entries in microsoft excel. I flip through pages and pages on this website, and on each page there are 50 links. In each link is a profile with...
2
by: Shannon Mayne | last post by:
I would like to ask how one might obtain the assigned name of an assigned object as a string. I would like to use object names as an algorithmic input. To demonstrate... So if i have: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.