Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,505 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,857 people online right now. Registration is fast and FREE... Join Now!




XML READER

 
Reply to this topicStart new topic

XML READER, having problem reading xml file

coolbhatia18
post 8 Oct, 2008 - 03:52 AM
Post #1


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 6

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.
CODE

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
CODE

<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>

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 12:33AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month