Introduction to ServerSide Web Development using JSP and

Introduction to Server-Side Web Development using JSP and XML Session V: Further JSP and integration with XML 18 th March 2004 Bogdan L. Vrusias b. vrusias@surrey. ac. uk 18 th March 2004 Bogdan L. Vrusias © 2004

Introduction to Server-Side Web Development Introduction • One of the most important concepts in building any application is the concept of data. • XML is a specification that gears data. • Data is stored in databases. • JSP and XML… a great way to access a database! 18 th March 2004 Bogdan L. Vrusias © 2004 2

Introduction to Server-Side Web Development Session V • • • What is XML? History of XML Introducing JSP and XML together Example using DOM Example using SAX Example using XSL 18 th March 2004 Bogdan L. Vrusias © 2004 3

Introduction to Server-Side Web Development What is XML? • "The e. Xtensible Markup Language (XML) is the universal format for structured documents and data on the Web" (W 3 C) • XML is not a markup language like HTML, it is a language used to describe a markup language (metalanguage). 18 th March 2004 Bogdan L. Vrusias © 2004 4

Introduction to Server-Side Web Development Applications of XML • The applications which XML can be applied to fall into four distinct groups: – Those where a distributed program has to interact with two or more sources of data, where the sources are expressed in different formats. – Applications which attempt to move a large amount of processing onto client computers. – Applications which require different users to have different views of the same data. – Applications in which objects are allowed to wander around a network carrying out specific functions. 18 th March 2004 Bogdan L. Vrusias © 2004 5

Introduction to Server-Side Web Development History of XML • The roots of XML can be found in the explosive growth of the World Wide Web in the mid-1990 s and in the browser wars that took place between the Microsoft Corporation and the Netscape Corporation, where each strived for dominance over the other with their respective browsers. • As the Web became larger and more users accessed it a number of problems were discovered by designers who used HTML: – The fact that a similar HTML source was displayed in different ways depending on what browser was used. – Some browser manufacturers developed HTML facilities which were not recognised by other browsers. – It was almost impossible to discern any semantics within a Web page: nowhere was this more apparent than in the use of search engines. 18 th March 2004 Bogdan L. Vrusias © 2004 6

Introduction to Server-Side Web Development History of XML • W 3 C decided in 1996 to develop a markup language which would eventually supersede HTML. The major goals of the language are: – That it should be easy to use in the Internet. – That it should be capable of supporting a large number of applications ranging from browsers to search engine databases. – That it should be compatible with SGML, the text processing language which was the inspiration for HTML. – That it should not be a complicated process to develop processors for documents written in languages defined in XML, for example it should be easy to write a program to check that a source text reflects its definition. – That the number of optional facilities of the language should be very low. – That XML documents should be easy to read and understand. – That documents written using a language defined by XML should be easy to develop using simple editors. • In 1998 the language, XML, was presented to the world as a final recommendation of the W 3 C; in effect it became an Internet standard. 18 th March 2004 Bogdan L. Vrusias © 2004 7

Introduction to Server-Side Web Development XML and Other Storage Media 18 th March 2004 Bogdan L. Vrusias © 2004 8

Introduction to Server-Side Web Development Server-side XML • One of the most aspiring aspects of XML is its simplicity. • Benefits: – XML simplifies data communications because it is self-describing. This makes it simple and easy to share XML data between people and applications. – Data storage and organisation is customisable. – XML supports Unicode therefore documents can be created in nearly any language. – XML is based on simple character set, therefore it makes it easy to transport an XML document. – Document structure can be AUTOMATICALLY validated. – XML can be mixed with stylesheets. – Any type of data can be described in XML. 18 th March 2004 Bogdan L. Vrusias © 2004 9

Introduction to Server-Side Web Development XML Example <ENTRY> <ENTRYPAIR> <NAME>Dodo</NAME> <DEFINITION> A dead bird</DEFINITION> </ENTRYPAIR> <NAME>Blackbird</NAME> <DEFINITION> A thieving bird</DEFINITION> </ENTRYPAIR> <NAME>Peacock</NAME> <DEFINITION> An attractive bird</DEFINITION> </ENTRYPAIR> </ENTRY> 18 th March 2004 Bogdan L. Vrusias © 2004 10

Introduction to Server-Side Web Development Defining XML-based Languages • The first thing that is needed when developing an XML application is to define the structure of the language that is to be processed. • The definition is known as a document type definition (DTD). • An example of a simple DTD is shown below: <? xml version = “ 1. 0” encoding =“UTF-8”? > <!DOCTYPE ENTRY[ <!ELEMENT ENTRYPAIR*> <!ELEMENT ENTRYPAIR (NAME, DEFINITION)> <!ELEMENT NAME (#PCDATA)> <!ELEMENT DEFINITION (#PCDATA)> ]> 18 th March 2004 Bogdan L. Vrusias © 2004 11

Introduction to Server-Side Web Development Processing XML-based Documents • The main tool that has been used by XML developers has been a parser. A parser is a program which takes the source of a language and checks that it correctly matches its definition. An XML parser will check an XML document against its DTD. • An XML parser can be a validating parser or a non-validating parser: – a validating parser checks an XML document adheres to every rule in its DTD; – a non-validating parser will make a smaller number of important checks such as the fact that a tag is matched by its end tag. • Well-Formed and Valid Documents – XML documents that follow all the XML syntax rules are referred to as “well-formed” documents. – A “valid” XML document is one that conforms to the DTD or schema written to describe its structure. 18 th March 2004 Bogdan L. Vrusias © 2004 12

Introduction to Server-Side Web Development Example of a Parser (Aelfred) … public class XMLApplet extends Applet implements Xml. Handler { public void init() { private Xml. Parser parser; parser = new Xml. Parser(); parser. set. Handler(this); . . } public void start() {. . } 18 th March 2004 Bogdan L. Vrusias © 2004 13

Introduction to Server-Side Web Development Example of a Parser (Aelfred) public void {…} public void (char[] {…} public void (String {…} … } 18 th March 2004 start. Document() end. Document() start. Element(String name) end. Element(String name) char. Data str. Val, int first, int last) attribute a. Name, String a. Value, boolean is. Specified) Bogdan L. Vrusias © 2004 14

Introduction to Server-Side Web Development Approaches to XML Processing • One of the problems with XML-based parsers is the fact that there has been no standardisation effort. • There a number of parsers each of which is implemented in different ways with different sets of methods. • In order to overcome this problem an API has been developed which calls a parser and then processes an internal form of the XML source that has been built up by it. • One such API known as SAX (Simple API for XML) • Another API is DOM (Document Object Model). 18 th March 2004 Bogdan L. Vrusias © 2004 15

Introduction to Server-Side Web Development SAX • The SAX API contains a number of methods which are similar to those found in Aelfred: – The method characters(char[] str. Val, int first, int last) The first argument contains a string and the two integers delineate the string within the array. The second argument indexes its first character and the third argument indexes the last character. This method is the same as char. Data in Aelfred. – The method start. Document is executed when a document is first read. – The method end. Document is executed when the end of an XML document is encountered. – The method start. Element(String name, Attribute. List list) will be executed whenever a start tag is encountered. It has two arguments: the first is a string which holds the name of the tag and the second is an object defined by the SAX class Attribute. List which holds a sequence of name/value pairs for each attribute. – The method end. Element(String name) will be executed whenever an end tag is encountered. It is associated with one argument which is a string that represents the name of the tag. 18 th March 2004 Bogdan L. Vrusias © 2004 16

Introduction to Server-Side Web Development DOM • The DOM model involves the storage of the XML source that is to be processed as a tree within memory. – Adding the packages org. w 3 c. dom. *, org. apache. xerces. parsers. DOMParser – Create the DOMParser object DOMParser parser = new DOMParser(); – Parse the XML document and get the Document object parser. parse(xml); Document doc = parser. get. Document(); – Call the list. Nodes method and pass the Node. List object. list. Nodes(doc. get. Child. Nodes(), out, ""); – Get necessary information from the Node. List object nlist. get. Length() nlist. item(i). get. Node. Name() nlist. item(i). get. Node. Type() nlist. item(i). get. Node. Value() 18 th March 2004 Bogdan L. Vrusias © 2004 17

Introduction to Server-Side Web Development SAX vs DOM • If you are writing Java programs which access XML <les you will be faced with the choice of what style of API to use: either the eventbased SAX approach or the tree processing approach of DOM. There a number of points to bear in mind when selecting which to use: – SAX is a conceptually simple way to program an XML application. It is based on events occurring when some element in an XML-defined source is encountered. – DOM is conceptually more difficult to program. It requires the programmer to develop code which traverses a tree made up of nodes and requires the use of recursion. – When an XML document is defined by a complex DTD, then programs using SAX can be very complex and somewhat unreadable. – DOM programs which access large DTDs are more readable and maintainable than SAX programs. – The memory demands of SAX are minimal. – The memory demands of DOM can be very high when the XML source is textually large. 18 th March 2004 Bogdan L. Vrusias © 2004 18

Introduction to Server-Side Web Development XSL • There is another popular way of processing source based on a concept known as XSL. • XSL (e. Xtensible Style Language) is an XML-based language used for defining the type of processing (like SAX and DOM). The language is often referred to as XSLT (T for Transformations) and was developed by the W 3 C. • XSL (e. Xtensive Stylesheet Language) is what we use to transform an XML document in HTML, XML or other. – – Namespaces (<xsl: template match=“/”>) Templates (<xsl: template match=“/”>) Whitespace and Encoding (non-braking space is ) Entity Declaration (<!DOCTYPE xsl: stylesheet [<!ENTITY nbsp “ ”>]>) – Trees, nodes and family – Xpath (<xsl: value-of select=“. ” />) 18 th March 2004 Bogdan L. Vrusias © 2004 19

Introduction to Server-Side Web Development XSL Example The XML: <? xml version = “ 1. 0”? > <? xml-stylesheet type = “text/xml” href = “example. xsl” ? > <BOOKLIST> <BOOK> <TITLE> An introduction to the saxophone </TITLE> <AUTHORS> E. J Wilson and R. Vitre </AUTHORS> <COMMENT> Good introductory stuff but ignores recent history post 1955 </COMMENT> </BOOK> … </BOOKLIST> 18 th March 2004 Bogdan L. Vrusias © 2004 20

Introduction to Server-Side Web Development XSL Example The XSL: <? xml version = “ 1. 0” ? > <xsl: stylesheet version = “ 1. 0” xmlns: xsl = “. . ”> <xsl: template match = “BOOKLIST”> <HTML><HEAD><TITLE> Generated HTML for the book </TITLE></HEAD><BODY> <xsl: apply-templates/> </BODY></HTML> </xsl: template> <xsl: template match = “BOOK”> <P> <xsl: value-of select = “TITLE”/> </P> </xsl: template> <xsl: stylesheet> 18 th March 2004 Bogdan L. Vrusias © 2004 21

Introduction to Server-Side Web Development The Relationship between XML and JSP • JSP is about character based output (moving data). • XML is about character based data (marking data). • Scenarios: – The data is stored within an XML file. JSP imports the data and converts it to HTML. – The data is stored within a database. Java process converts the data to XML. Then either the XML is shipped to the user, or transformed to other formats (e. g. HTML) – The data is stored within an XML file. Java process stores the data to a database. JSP then is used to access and view the data. – Web application initialisation information is stored within an XML file. JSP reads the XML and changes application behaviour. – Data is transferred between two systems in an XML-formatted file. JSP is used as a front-end interface to initiate the process of sending or receiving data. 18 th March 2004 Bogdan L. Vrusias © 2004 22

Introduction to Server-Side Web Development A Warning • XML is not the most appropriate solution to all problems. • If misused, XML will slow down Web applications, waste development time and cost money. • XML does NOT replace a database. • Do not use XML for XML’s sake 18 th March 2004 Bogdan L. Vrusias © 2004 23

Introduction to Server-Side Web Development Tools for XML • Many tools exist for the processing and transformation of XML. We will evaluate the APIs that are most commonly used with JSP. – Xerces (XML Parser) • Xerces is an XML parser and generator. • It implements the W 3 C XML and DOM, as well as the SAX standard. – Xalan (XSLT Processor) • Xalan is an XSLT stylesheet processor. • It implements the W 3 C XSLT and XPath recommendations. – JAXP (Generic XML Processing) • JAXP is technically an API for XML processing. • It contains no parsing functionality (it’s an abstraction layer) • It does not provide new functionality to XML processing, except that it provides a common way to use different XML processor implementations (such as DOM and SAX). 18 th March 2004 Bogdan L. Vrusias © 2004 24

Introduction to Server-Side Web Development Java XML / XSL APIs • DOM (XML Document Object Model) – Easy to use, but requires lots of memory. • SAX (XML Parser) – More complicated to use, but is more efficient. • JDOM (XML Document Representation) (for Java Only) – JDOM has several classes that permit it to use SAX or the DOM. – Does not support XML parsing. • dom 4 j (XML Document Representation) (for Java Only) – dom 4 j has a set of Java APIs used for handling XML, XSLT and XPath. – Similar to JDOM, however it also differs as it uses Java interfaces. • JAXB (Parser and XML Document Representation) – Java Architecture for XML Binding (JAXB) is used to automate mapping between XML and Java. – It uses XML schema to build an optimised Java class to perform the parsing. 18 th March 2004 Bogdan L. Vrusias © 2004 25

Introduction to Server-Side Web Development Session V: Closing • • Questions? ? ? Remarks? ? ? Comments!!! Evaluation! 18 th March 2004 Bogdan L. Vrusias © 2004 26
- Slides: 26