XML READER

having problem reading xml file

Page 1 of 1

0 Replies - 1738 Views - Last Post: 08 October 2008 - 04:52 AM Rate Topic: -----

#1 coolbhatia18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-July 08

XML READER

Posted 08 October 2008 - 04:52 AM

i am trying for a function in wich when i pass an xml file and name of node for which i want to read data.this data is stored in record variable which i will use to generate csv. the code accesses all the nodes and child nodes but i am unable to write code to get the data of these labels in record function.
from xml.dom.minidom import parse
import xml.dom.minidom
from xml.dom.minidom import Node


def read_xml(filename, record_root):
	doc = xml.dom.minidom.parse(filename)
	recordRootNode = doc.getElementsByTagName(record_root)[0]

	fields = read_xml_record(recordRootNode).keys()
	records = [fields]
	for node in recordRootNode.childNodes:
		record_dict = read_xml_record(node)
		record_list = []
		for field in fields:
			if record_dict.has_key(field):
				record_list.append(record_dict[field])
			else:
				record_list.append(None)
		records.append(record_list)
		return records

def read_xml_record(record_node):
	record = {}
	choices =[e for e in record_node.childNodes if e.nodeType==e.ELEMENT_NODE]
	#print choices
		if ((not (node.nodeType == Node.TEXT_NODE)) and (not (node.nodeType == Node.CDATA_SECTION_NODE))):
	for node in choices:			  
		record[node.data] = node.childNodes[0].data
	return record


myDoc=read_xml("page1.xml",'page')



need HELP!!!

ps: this is the sample xml file
 <page>
	<title>Darsheel Safary</title>
	<id>15276803</id>
	<revision>
	  <id>236885217</id>
	  <timestamp>2008-09-07T16:24:50Z</timestamp>
	  <contributor>
		<username>Shshshsh</username>
		<id>2789676</id>
	  </contributor>
	  <minor/>
	  <comment>Reverted edits by [[Special:Contributions/Karunsheel05|Karunsheel05]] ([[User talk:Karunsheel05|talk]]) to last version by BOLLYWOOD DREAMZ</comment>
	  <text xml:space="preserve">{{Infobox actor
| name		  = Darsheel Safary
| image		 = DarsheelSafary.jpg
| imagesize	 =
| caption	   = Darsheel Safary on [[Sa Re Ga Ma Pa L'il Champs International]] (2007).
| birthname	 =
| birthdate	 = {{birth date and age|1996|3|9}}
| location	  = [[India]]
| occupation	= [[Film actor]]
| yearsactive   = 2007-present
| spouse		=
| homepage	  =

}}
'''Darsheel Safary''' (born March 9, 1996) is an award winning [[India]]n film actor working in [[Hindi]] [[Bollywood]] films. He is best known for his highly critically acclaimed portrayal of a [[dyslexic]] child in the 2007 film ''[[Taare Zameen Par]]''.&lt;ref name=&quot;NFA&quot;&gt;{{cite web|title=&quot;Darsheel deserves National Award for
[[Category:Indian child actors]]
[[Category:Living people]]</text>
	</revision>
  </page>



Is This A Good Question/Topic? 0
  • +

Page 1 of 1