Listing 15.1[em]A document-start Rule
document-start ;event
output "%n" ;our action
|| "
%n"
|| "
Amsterdam
Paris
Toronto
Vancouver
Brussels
Listing 15.13[em]nrofcities.xom[md]Using referents.
global counter nrofcities initial {0} ; a variable with
name "nrofcities" and value "0"
element cities ;rule
output "%c" ; continue parsing
element city
increment nrofcities ; the variable "profcities"
is incremented (+1)
output ""
output "City %d(nrofcities) of " ; the value of
variable "nrofcities" is sent to the output
output referent "totalofcities"
; the referent is the placeholder for the
not known yet value of the total of cities
; the content of the placeholder is sent to
the output
output "
"
output "%c"
output "
"
set referent "totalofcities" to "%d(nrofcities)"
; the value of "nrofcities" is placed in
the placeholder
; after the first its 1, then 2, and
finally for our example 5
Listing 15.14[em].biblio.xml[md]The xml File to Convert to HTML
]>
Sam's Teach Yourself C++ in 21 Days, Second Edition
Jesse Liberty
This book teaches you the basics of
object-oriented programming with C++ and is completely
revised to ANSI standards. It can be used with any
C++ compiler.
0-672-31070-8
700
Beginning - Intermediate
29.99
Teach Yourself Visual C++ 5
Maximum Java 1.1
Glenn Vanderburg
Written by JAVA experts, this book explores
the JAVA 1.1 language, tools, and core JAVA API without
reviewing fundamentals or basic techniques.
1-57521-290-0
900
Expert
49.99
JavaScript Unleashed, Second Edition
Richard Wagner
This book helps you thoroughly understand
and apply JavaScript.
1-57521-306-0
1000
Casual - Experienced
49.99
Sam's Teach Yourself Visual C++ 5 in 21 Days,
Fourth Edition
Nathan Gurewich
Ori Gurewich
This book merges the power of the best-selling
"Teach Yourself" series with the knowledge of Nathan and
Ori Gurewich, renowned experts in code, creating the most
efficient way to learn Visual C++.
0-672-31014-7
832
New - Casual
35.00
Jesse Liberty
C
Glenn Vanderburg
Java
Richard Wagner
Opera
Nathan Gurewich
Visual Basic
Visual C++
Listing 15.15[em]tohtml.xml[md]Our Omnimark Script to Convert the File biblio.xml to HTML
DOWN-TRANSLATE with XML
element biblio
output "%n"
|| "%n"
|| "My bookshop%n"
|| "%n"
|| "%n"
|| "My bookshop
%n
%n"
|| "%c" ; continue parsing
element books
output "%c"
element book
output "%c"
do when last subelement is related
output ""
done
output "
%n"
element title
output ""
using attribute id of parent
do when attribute id is specified
output ""
done
output "%c
%n"
element author
do when previous is title
output "%c"
else
output "
%n"
|| "%c"
done
element description
output "
%n" ; closing the p element started
with the first author
|| "
%n"
|| "%c
%n"
element ISBN
output "ISBN-number: %c
%n"
element pages
output "%c pages
%n"
element targetgroup
output "Aimed at the '%c'
%n"
element price
output "%c US$
%n"
element related
do when occurrence = 1
output "See also:
"
done
output "%c
"
element authors
output "
An overview of
our writers
%n"
output ""
output "Name | Specialty |
%n"
output "%c
%n"
element authordesc
output "%c
%n"
element name
output "%c | %n"
element specialty
do when occurrence = 1
output "%c"
else
output " - %c"
done
document-end
output ""
Listing 15.16[em]tohtml.xml[md]Conversion to Multiple HTML Files
element book
local stream bookhtm ;creating a variable with
name "bookhtm"" of type stream
open bookhtm as file "%v(id).htm"
; this stream is written to a file,
based on the id of the book element
using output as bookhtm ; now we write the output to
this stream (file)
do ;everything between 'do' and 'done' needs to go to the stream
output "%n"
|| output "%n"
|| output "Book %v(id)"%n
|| output "%n"
|| output "%n"
output "%c"
do when last proper subelement is related
output ""
done
output " %n"
output "%n"
done
element authors
local stream authorshtm
open authorshtm as file "authors.htm"
using output as authorshtm
output "%n"
|| output "%n"
|| output "Authors"%n
|| output "%n"
|| output "%n"
output "An overview of our writers%n"
output ""
output "Name | Specialty | %n"
output "%c %n"
output "%n"
done
Listing 15.17[em]tohtml.xml[md] Adding a TOC.
global stream toc; to be used later on as a referent
element biblio
output "%n"
|| "%n"
|| "My bookshop%n"
|| "%n"
|| "%n"
|| "My bookshop%n%n"
output referent "toc" ; here we will (out)put our toc
open toc as referent "toc" ; and we initialise it
put toc "
Table of Contents%n"
close toc
output "%c" ; continue parsing
Listing 15.18[em]tohtml.xml[md] Filling the TOC
element title
; putting the content into this variable since we
will use this content twice
local stream titlecontent ; creating a variable of
type stream
open titlecontent as buffer
put titlecontent "%c"
close titlecontent
; existing code
output ""
using attribute id of parent
do when attribute id is specified
output ""
done
output "%g(titlecontent)%n"
; referent
reopen toc as referent "toc"
using attribute id of parent
put toc "- %g(titlecontent)
"
close toc
Listing 15.19[em]tohtml.xml[md] Including Link to Previous
element book
local stream bookhtm
open bookhtm as file "%v(id).htm"
using output as bookhtm
do
output "%c"
do when last proper subelement is related
output ""
done
output " %n"
output "%g(prevpage)" ; here we add our previous link
set prevpage to "Previous "
; and we keep the information of this page for the next one
done
Listing 15.20[em]tohtml.xml[md] Including Link to Next
element book
local stream bookhtm
set referent "next-%v(id)" to ""
; the next on this page is set to empty
; this is done since we don't know yet if there is a next page
set referent "next-%g(previd)" to "Next"
; the next of previous page gets the info of this (being next) page
open bookhtm with referents-allowed as file "%v(id).htm"
; referents-allowed added since we put placeholders in this stream now
using output as bookhtm ; writing to the file
do
output "%c"
do when last proper subelement is related
output ""
done
output " %n"
output "%g(prevpage)" ; the info of previous page
output referent "next-%v(id)" ; our placeholder
set prevpage to "Previous "
set previd to "%v(id)" ; used in next page for filling in placeholder
done
Listing 15.21[em]Subclassing the Class HandlerBase.
public class OurHandler extends HandlerBase { //subclassing HandlerBase
public void startDocument ()
{
//our implementation of the method, overriding HandlerBase
}
public void endDocument ()
{
//our implementation of the method, overriding HandlerBase
}
public void startElement (String name, AttributeList atts)
{
//our implementation of the method, overriding HandlerBase
}
public void endElement (String name)
{
//our implementation of the method, overriding HandlerBase
}
public void characters (char ch[], int start, int length)
{
//our implementation of the method, overriding HandlerBase
}
}
Listing 15.22[em]musicians.xml[md]Info about Musicians
Joey Baron
drums
1
Bill Frisell
guitar
3
Don Byron
clarinet
2
Dave Douglas
trumpet
1
Listing 15.23[em]OurHandler.java[md]Concrete Implementations of Event Handlers.
import org.xml.sax.HandlerBase;
import org.xml.sax.AttributeList;
import java.io.*;
public class OurHandler extends HandlerBase { //subclassing HandlerBase
private PrintWriter fout;
public OurHandler() throws IOException
{
fout = new PrintWriter(new FileWriter("out.htm"));
//object created for writing to the file "out.htm"
}
public void startDocument ()
{
fout.println("");
fout.println(" SAX example");
fout.println("");
}
public void endDocument ()
{
fout.println("");
fout.close();
}
public void startElement (String name, AttributeList atts)
{
if (name == "musicians")
fout.println("");
else if (name == "musician")
fout.println("");
else
fout.println("");
}
public void endElement (String name)
{
if (name == "musicians")
fout.println(" | ");
else if (name == "musician")
fout.println("");
else
fout.println(" | ");
}
public void characters (char ch[], int start, int length)
{
for (int i=start; i < start+length; i++)
fout.print(ch[i]);
}
}
Listing 15.24[em]convert.java[md]The Program that uses OurHandler Class
import org.xml.sax.Parser;
import org.xml.sax.DocumentHandler;
import org.xml.sax.helpers.ParserFactory;
public class convert {
static final String parserClass = "com.ibm.xml.parser.SAXDriver";
//using IBM's XML parser
//static final String parserClass = "com.datachannel.xml.sax.SAXDriver";
//in case we want to use the DXP parser
static final String xmlfile="c:\\xmlex\\musicians.xml";
public static void main (String args[]) throws Exception
{
Parser parser; //variable declaration
//the name parser will be used to refer to a Parser object
DocumentHandler handler; //variable declaration
//the name handler will be used to refer to a DocumentHandler object
parser = ParserFactory.makeParser(parserClass);
//A Parser object is created by supplying a class name to the ParserFactory
handler = new OurHandler();
//The new object is created and initialized
parser.setDocumentHandler(handler);
//handler is registered with the parser
parser.parse(xmlfile);
//our xml-file is parsed
}
}
--------------------------------------------------------------
(c)Exercises
Start document
Start element: musicians
Start element: musician
Start element: name
Characters: Joey Baron
End element: name
Start element: instrument
Characters: drums
End element: instrument
Start element: NrOfRecordings
Characters: 1
End element: NrOfRecordings
End element: musician
…
End element: musicians
End document
---------------------------------------------------------------------------
You can use the following template:
import org.xml.sax.HandlerBase;
import org.xml.sax.AttributeList;
public class YourHandler extends HandlerBase {
public void startDocument ()
{
System.out.println("XXXXX");
}
public void endDocument ()
{
System.out.println("XXXXX");
}
public void startElement (String name, AttributeList atts)
{
System.out.println("XXXXX");
}
public void endElement (String name)
{
System.out.println("XXXXX");
}
public void characters (char ch[], int start, int length)
{
System.out.println("XXXXX");
}
}