[Expat-discuss] How to get the value of an element
Bin Chen
binary.chen at gmail.com
Thu May 24 09:22:08 CEST 2007
Thanks, but that's not I want, I translate below code to C and run, then
find many useless char is also captured. I don't need any other useless
char other than the value of f2, is there any better way to do this?
The CharacterDataHandler seems a handler for every char that not
processed by "element handler", is it right? So something like '\t' and
'\n' are also be handled, and it's totally context free.
I want the content of f2, it is context sensitive, there are no API for
this?
在 2007-05-24四的 12:28 +0530,Mukesh Kumar写道:
>
>
> 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