Workshop 2

Workshop 2

Firstly, I created file, WML_create_miniDOM.py, as per instruction. Then executed the file by using IDLE but returned syntax error.

Then we changed the “From” to “from” in the document and run again with positive result as follows:

 

<?xml version=”1.0” ?>

<wml>

  <card id=”main”>

    <p>

      XML for e-business!

    </p>

  </card>

</wml>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

I created the file, native_plants.xml, as per instruction. Then, I modified the poetry.py as follows and save as native_plants_SAXparser.py. Lastly, I executed the file, native_plants_SAXparser.py, by IDLE and returned the result as below.

 

Code of “native_plants_SAXparser.py”

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#!/usr/local/bin/python

 

# Defining my specialized handler classes

from xml.sax.handler import ContentHandler

 

import xml.sax, sys

 

class textHandler(ContentHandler):

 

           def characters(self, ch):

 

                     sys.stdout.write(ch)

 

# Creating an XML parser

parser = xml.sax.make_parser()

 

# Creating an instance of the handler classes

handler = textHandler()

 

# Telling the parser to use your handler instance

parser.setContentHandler(handler)

 

# Parsing the file; my handler’s method will get called

parser.parse(“native_plants.xml”)

 

Result:

 

Nepenthes burbidgeae

 

Nusa rubinea

 

Drosera spatulata

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.