Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Server-Side Web Development Introduction to Server-Side Web Development using JSP and XML Session V: Further JSP and integration with XML.

Similar presentations


Presentation on theme: "Introduction to Server-Side Web Development Introduction to Server-Side Web Development using JSP and XML Session V: Further JSP and integration with XML."— Presentation transcript:

1 Introduction to Server-Side Web Development 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

2 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 20042 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!

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

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

5 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 20045 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.

6 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 20046 History of XML The roots of XML can be found in the explosive growth of the World Wide Web in the mid-1990s 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 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.

7 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 20047 History of XML W3C 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 W3C; in effect it became an Internet standard.

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

9 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 20049 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.

10 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200410 XML Example Dodo A dead bird Blackbird A thieving bird Peacock An attractive bird

11 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200411 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: <!DOCTYPE ENTRY[ ]>

12 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200412 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.

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

14 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200414 Example of a Parser (Aelfred) public void startDocument() {…} public void endDocument() {…} public void startElement(String name) {…} public void endElement(String name) {…} public void charData (char[] strVal, int first, int last) {…} public void attribute (String aName, String aValue, boolean isSpecified) {…} … }

15 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200415 Approaches to XML Processing One of the problems with XML-based parsers is the fact that there has been no standardisation effort. There are 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).

16 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200416 SAX The SAX API contains a number of methods which are similar to those found in Aelfred: –The method characters (char[] strVal, 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 charData in Aelfred. –The method startDocument is executed when a document is first read. –The method endDocument is executed when the end of an XML document is encountered. –The method startElement( String name, AttributeList 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 AttributeList which holds a sequence of name/value pairs for each attribute. –The method endElement( 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.

17 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200417 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.w3c.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.getDocument(); –Call the listNodes method and pass the NodeList object. listNodes(doc.getChildNodes(), out, ""); –Get necessary information from the NodeList object nlist.getLength() nlist.item(i).getNodeName() nlist.item(i).getNodeType() nlist.item(i).getNodeValue()

18 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200418 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 event- based SAX approach or the tree processing approach of DOM. There are 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.

19 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200419 XSL There is another popular way of processing source based on a concept known as XSL. XSL (eXtensible 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 W3C. XSL (eXtensive Stylesheet Language) is what we use to transform an XML document in HTML, XML or other. –Namespaces ( ) –Templates ( ) –Whitespace and Encoding (non-braking space is  ) –Entity Declaration ( ]>) –Trees, nodes and family –Xpath ( )

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

21 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200421 XSL Example The XSL: Generated HTML for the book

22 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200422 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.

23 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200423 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

24 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200424 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 W3C XML and DOM, as well as the SAX standard. –Xalan (XSLT Processor) Xalan is an XSLT stylesheet processor. It implements the W3C 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).

25 Introduction to Server-Side Web Development 18 th March 2004Bogdan L. Vrusias © 200425 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. dom4j (XML Document Representation) (for Java Only) –dom4j 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.

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


Download ppt "Introduction to Server-Side Web Development Introduction to Server-Side Web Development using JSP and XML Session V: Further JSP and integration with XML."

Similar presentations


Ads by Google