Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet296/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   292   293   294   295   296   297   298   299   ...   453
startElement()
: The parser calls this method at the beginning of an element.


endElement()
: The parser calls this method at the end of an element.


characters()
: Every character read from the source generates a call to 
characters()
.
For this example, the 
startElement()
method checks the element name. If the element is a 
‘Message‘
element, then the code sets 
IsMessage
to 
True
and clears 
Message
of any existing content. This is a 
preparatory step.
When the 
characters()
method sees that 
IsMessage
is 
True
, it appends every character it receives 
to 
Message
. Remember that these are individual characters, so you can’t assume much about the 
content except that the flow is from the beginning of the file to the end of it. In other words, you 
won’t receive characters out of order.
The 
endElement()
checks the element name again. When the element name is 
‘Message‘
, the code 
prints the content of 
Message
. Because 
Message
contains all of the characters from the source, you 
548592c13.indd 285
2/24/10 12:49:00 PM
www.finebook.ir


286

CHAPTER 13
Working With XML Data
must use 
strip(‘\n\t‘)
to remove any control characters. The output from this example is the 
same as shown in Figure 13-2.
Now that you understand the handler, it’s time to see how you put it to work. The main part of 
the code begins by creating a parser, 
Parser
, using 
xml.sax.make_parser()
. Remember that the 
parser simply generates events based on the input characters it sees. The handler performs the actual 
interpretation of those characters.
The next step is to create an instance of 
MessageHandler
named 
Handler
. The code uses 
Parser 
.setContentHandler()
to assign the handler to 
Parser
. Otherwise, 
Parser
won’t know which 
handler to use to process the XML characters.
In order to process the XML file, the code still requires a source — the third construct. The 
open(‘Test2.XML‘)
call opens 
Test2.XML
as a source and passes this source to 
Parser
through 
the 
Parser.parse()
method. It’s the call to the 
Parser.parse()
method that actually begins 
the process of generating events.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   292   293   294   295   296   297   298   299   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish