[Expat-discuss] How to get the value of an element

Mukesh Kumar Mukesh.S at mphasis.com
Thu May 24 08:58:39 CEST 2007


 

Hi,

 Try this

 

 

import xml.parsers.expat

 

# 3 handler functions

def start_element(name, attrs):

    print 'Start element:', name, attrs

def end_element(name):

    print 'End element:', name

def char_data(data):

    print 'Character data:', repr(data)

 

p = xml.parsers.expat.ParserCreate()

 

p.StartElementHandler = start_element

p.EndElementHandler = end_element

p.CharacterDataHandler = char_data

 

p.Parse("""<?xml version="1.0"?>

<parent id="top"><child1 name="paul">Text goes here</child1>

<child2 name="fred">More text</child2>

</parent>""")

 

 

You can you a file to read the xml contents and read the xml document
and in  a while loop , and extract the contents.

 

 

Here is my small web-page:

http://www.geocities.com/muki_champs

 

Regards,

Mukesh Srivastav,

Sr.Software Engineer,

India,

Bangalore.

 

-----Original Message-----
From: expat-discuss-bounces at libexpat.org
[mailto:expat-discuss-bounces at libexpat.org] On Behalf Of Bin Chen
Sent: Thursday, May 24, 2007 12:07 PM
To: expat-discuss at libexpat.org
Subject: [Expat-discuss] How to get the value of an element

 

Hi,

 

For a xml like:

 

<f1>

        <f2>

                abc

        </f2>

</f1>

 

 

The vaule of element f2 is abc, now I want to get this value, how can?

 

Thanks a lot.

 

Bin

 

_______________________________________________

Expat-discuss mailing list

Expat-discuss at libexpat.org

http://mail.libexpat.org/mailman/listinfo/expat-discuss



More information about the Expat-discuss mailing list