Top Banner
Advanced Programming Rabie A. Ramadan Lecture 3
94

Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Dec 13, 2015

Download

Documents

Austen Houston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Advanced Programming

Rabie A. Ramadan

Lecture 3

Page 2: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network Programming An Overview

2

Page 3: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Computer Network

A computer network is an interconnected collection of autonomous computers.

Page 4: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology A topology is a way of “laying out” the network.

Topologies can be either physical or logical.

Physical topologies describe how the cables are run.

Logical topologies describe how the network messages travel

Page 5: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

Bus (can be both logical and physical)

Star (physical only)

Ring (can be both logical and physical)

Mesh (can be both logical and physical)

Page 6: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

Bus A bus is the simplest physical topology. It consists of a single

cable that runs to every workstation This topology uses the least amount of cabling, but also covers

the shortest amount of distance.

Page 7: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

It is difficult to add a workstation

Have to completely reroute the cable and possibly run two additional lengths of it.

If any one of the cables breaks, the entire network is disrupted. Therefore, it is very expensive to maintain.

Page 8: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

Star Topology A physical star topology branches each network device off a

central device called a hub, making it very easy to add a new workstation.

Also, if any workstation goes down it does not affect the entire network.

Page 9: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

Star topologies are more expensive to install than bus networks,• There are several more cables that need to be installed,

plus the cost of the hubs that are needed.

Page 10: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

Ring Each computer connects to two other computers,

joining them in a circle creating a unidirectional path where messages move workstation to workstation.

Page 11: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

The ring makes it difficult to add new computers.

Unlike a star topology network, the ring topology network will go down if one entity is removed from the ring.

Physical ring topology systems don’t exist much anymore, mainly because the hardware involved was fairly expensive and the fault tolerance was very low.

Page 12: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

Mesh The mesh topology is the simplest logical topology in

terms of data flow, but it is the most complex in terms of physical design.

In this physical topology, each device is connected to every other device

Page 13: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.)

The physical mesh topology is very expensive to install and maintain.

Cables must be run from each device to every other device. The advantage you gain from it is its high fault tolerance.

There will always be a way of getting the data from source to destination.

Page 14: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network topology (cont.) Advantages and Disadvantages of Network Topologies

Topology Advantages Disadvantages

Bus Cheap. Easy to install. Difficult to reconfigure.

Break in bus disables

entire network.

Star Cheap. Easy to install.

Easy to reconfigure.

Fault tolerant.

More expensive than bus.

Ring Efficient. Easy to install. Reconfiguration difficult.

Very expensive.

Mesh Simplest. Most fault tolerant. Reconfiguration extremely difficult.

Extremely expensive.

Very complex.

Page 15: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Computer Network

A network includes:• Special purpose hardware devices that:

• Interconnect transmission media

• Control transmission of data

• Run protocol software

• Protocol software that:• Encodes and formats data

• Detects and corrects problems encountered during transmission

Page 16: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Addressing and Routing

Address: byte-string that identifies a node• usually unique

Routing: process of forwarding messages to the destination node based on its address

Types of addresses• unicast: node-specific

• broadcast: all nodes on the network

• multicast: some subset of nodes on the network

Page 17: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

IP Addresses and Classes

IPv6 addresses have a size of 128 bits

Page 18: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

IP Addresses and Classes

Page 19: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

IP Addresses and Classes

Page 20: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

IP Addresses and Classes

20

Page 21: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Subnet Mask

21

Page 22: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network Architecture A network architecture is a set of layers and protocols used

to reduce network design complexity.

The TCP/IP Protocol Suite (also called the Internet Architecture) is an important example of a network architecture.

The OSI (Open Systems Interconnection) 7-Layer Reference Model [ISO,1984] is a guide that specifies what each layer should do, but not how each layer is implemented.

Page 23: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

ISO/OSI Reference Model

Page 24: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Application

Presentation

Session

Transport

End host

One or more nodes

Network

Data link

Physical

Network

Data link

Physical

Network

Data link

Physical

Application

Presentation

Session

Transport

End host

Network

Data link

Physical

within the network

ISO 7-Layer Reference Model

Page 25: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

TCP/IP Model

25

Page 26: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Internet Model

26

Page 27: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Protocols A protocol is a set of rules of communication. Protocols are the building blocks of a network architecture.

Term “protocol” is overloaded

• specification of peer-to-peer interface

• module that implements this interface

Page 28: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Network Programming

A network allows arbitrary applications to communicate.

However, a network programmer doesn’t need to know the details of all lower-level network technologies.

Network facilities are accessed through an Application Programming Interface (API); e.g., a Service Interface.

Page 29: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Basic Paradigm for Communication

Most network applications can be divided into two pieces: a client and a server.

A Web browser (a client) communicate with a Web server.

A Telnet client that we use to log in to a remote host.

A user who needs access to data located at remote server.

Page 30: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Basic Paradigm for Communication

Establish contact (connection). Exchange information (bi-directional). Terminate contact.

Page 31: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Client-Server Paradigm Server waits for client to request a connection.

Client contacts server to establish a connection.

Client sends request.

Server sends reply.

Client and/or server terminate connection.

Page 32: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Two types of Communication

Connection-oriented• Setup the link before communication.

• Similar to the phone call. We need the phone number and receiver.

Connectionless• No link needed to be set up before communication.

• Similar to send a letter. We need the address and receiver.

Page 33: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

TCP and UDP

TCP (Transmission Control Protocol) is a connection-oriented protocol.

UDP (User Datagram Protocol) is connectionless (UDP) protocol.

Page 34: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Ports

Identifying the ultimate destination IP addresses identify hosts Host has many applications Ports (16-bit identifier)

192.18.22.13

Port 80 25 23

Application WWW E-mail Telnet

Page 35: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sockets A socket is defined as an endpoint for communication.

Concatenation of IP address and port

A socket pair (local IP address, local port, foreign IP address, foreign port) uniquely identifies a communication.

The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8

Page 36: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sockets

36

Page 37: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sockets and Ports

message

agreed portany port socketsocket

Internet address = 138.37.88.249Internet address = 138.37.94.248

other ports

client server

Page 38: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

TCP Socket

Bind() binds the socket to the specified address. The address parameter specifies the local component of the address, e.g. IP address and UDP/TCP port

Page 39: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

UDP Socket

Page 40: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

40

Page 41: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Steps in creating Clients in Java

41

What is the difference between String and BufferedReader?

Page 42: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Steps in Creating Clients in Java

42

Page 43: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

43

Page 44: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

44

Page 45: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

45

Page 46: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

46

Page 47: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

47

Page 48: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

48

Page 49: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

49

Page 50: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

50

Page 51: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

51

Page 52: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

52

Page 53: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

53

Page 54: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

References

http://www.sockets.com/winsock.htm http://tangentsoft.net/wskfaq/ Core Web Programming book

Chapter 17. Network Programming

Page 55: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Dynamic Programming

04/18/23 55

Page 56: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Dynamic Programming

04/18/23 56

JavaScript …. Server-Side Java: Servlets XML Remote Method Invocation (RMI) Java Server Pages (JSP) Others… Reference

Marty Hall, Larry Brown, Core Web Programming, Second Edition.

Page 57: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

What are Servelts?

04/18/23 57

Servlets are programs that run on a web server,

Act as a middle layer between a request coming from a web browser or other http client and databases or applications on the http server.

Page 58: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

What is their Job?

04/18/23 58

Read any data sent by the user. • A form on a Web page

• An applet or

• A custom HTTP client program.

Look up any other information about the request that is embedded in the HTTP request. • This information includes details about browser capabilities, cookies, the

host name of the requesting client, and so forth.

Generate the results. • This process may require talking to a database, invoking a legacy

application, or computing the response directly.

Page 59: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

What is their Job?

04/18/23 59

Format the results inside a document. • In most cases, this involves embedding the information inside an

HTML document.

Set the appropriate HTTP response parameters.

• This means telling the browser what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.

Send the document back to the client. • This document can be sent in text format (HTML), binary format (GIF

images), or even in a compressed format like gzip that is layered on top of some other underlying format.

Page 60: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Common Gateway Interface (CGI)

04/18/23 60

Is Simply a program interprets the Client request to the server and forms the response back to the client .

A new process is created for each request.

Page 61: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Servlet vs. CGI

CGIBased

Webserver

CGIBased

Webserver

Request CGI1Child for CGI1

CGIBased

Webserver

Servlet Based Webserver

JVM

Request CGI1Child for CGI1

Request Servlet1

CGIBased

Webserver

Servlet Based Webserver

JVMServlet1

Request CGI1Child for CGI1

Request CGI2

Request Servlet1

CGIBased

WebserverChild for CGI2

Servlet Based Webserver

JVMServlet1

Request CGI1Child for CGI1

Request CGI2

Request Servlet1

Request Servlet2

CGIBased

WebserverChild for CGI2

Servlet Based Webserver

JVMServlet1

Servlet2

Request CGI1Child for CGI1

Request CGI2

Request CGI1

Request Servlet1

Request Servlet2

CGIBased

WebserverChild for CGI2

Child for CGI1

Servlet Based Webserver

JVMServlet1

Servlet2

Request CGI1Child for CGI1

Request CGI2

Request CGI1

Request Servlet1

Request Servlet2

Request Servlet1

CGIBased

WebserverChild for CGI2

Child for CGI1

Servlet Based Webserver

JVMServlet1

Servlet2

Request CGI1Child for CGI1

Page 62: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Advantages of Servlets Over "Traditional" CGI

04/18/23 62

Efficient• Uses threads instead of O.S. processes for each request

Convenient• Ready made components to automatically parse and decode HTML

form data, read and set HTTP headers, handle cookies, track sessions, and many other such high-level utilities

Powerful• Can talk directly to HTTP server ,

• Multiple servlets can also share data, making it easy to implement database connection pooling and similar resource-sharing optimizations

Page 63: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Advantages of Servlets Over "Traditional" CGI

04/18/23 63

Portable• By the end of the day it is java

Secure• Filter out characters such as backquotes and semicolons that are

treated specially by the shell

• No Buffer overflow

Inexpensive• Free of charge

Page 64: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

64

Servlet Servlet

Page 65: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Basic Servlet Structure

04/18/23 65

Page 66: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Basic Servlet Structure Analysis

04/18/23 66

To be a servlet, a class should extend HttpServlet and override doGet or doPost, depending on whether the data is being sent by GET or by POST.

HttpServletRequest has methods by which you can find out about incoming information such as form (query) data, HTTP request headers, and the client's hostname.

The HttpServletResponse lets you specify outgoing information such as HTTP status codes (200, 404, etc.) and response headers (Content-Type, Set-Cookie, etc.).

It lets you obtain a PrintWriter with which you send the document content back to the client.

Page 67: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Handler Functions

67

Each HTTP Request type has a separate handler function.

• GET -> doGet(HttpServletRequest, HttpServletResponse)

• POST -> doPost(HttpServletRequest, HttpServletResponse)

• DELETE -> doDelete (HttpServletRequest, HttpServletResponse)

• TRACE -> doTrace (HttpServletRequest, HttpServletResponse)

• OPTIONS -> doOptions (HttpServletRequest, HttpServletResponse)

Page 68: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Generating HTML

04/18/23 68

Page 69: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Generating Simple HTML Page

04/18/23 69

Page 70: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Analysis

04/18/23 70

response.setContentType("text/html");

You can use it to generate other document types :

GIF images (content type image/gif)

Excel spreadsheets (content type application/vnd.ms-excel).

Page 71: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Servlet Life Cycle

71

Page 72: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

A Servlet Template

72

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class ServletTemplate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Use "request" to read incoming HTTP headers // (e.g. cookies) and HTML form data (e.g. data the user // entered and submitted). // Use "response" to specify the HTTP response status // code and headers (e.g. the content type, cookies). PrintWriter out = response.getWriter(); // Use "out" to send content to browser }}

Page 73: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Hello World Servlet

73

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWWW extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html");

PrintWriter out = response.getWriter();out.println("<HTML>\n" + "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" + "<BODY>\n" + "<H1>Hello WWW</H1>\n" + "</BODY></HTML>");

}}

Page 74: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Your Assignment

04/18/23 74

Read chapter 19 from the “Core Web Programming” book and implement the last example of the chapter (19.12 Session tracking)

Page 75: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Web Programming (continue)

Page 76: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

76

HTML Basics HTML Basics

Page 77: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Basic HTML Document Format

77

<HTML><HEAD> <TITLE>WENT'99</TITLE></HEAD><BODY> Went'99 </BODY> </HTML>

<HTML><HEAD> <TITLE>WENT'99</TITLE></HEAD><BODY> Went'99 </BODY> </HTML>

See what it looks like:

Page 78: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

HTML 5 Parsing

78

Reverse engineering current browsers

Compatible with existing browsers and web pages

Thoroughly defines error handling

Page 79: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

<header>

<footer>

<section><nav> <aside>

<article>

<div id="header">

<div id="footer">

<div id="content"><divid="nav">

<divid="right">

<div class="article">

New Structures

Page 80: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

<progress>

<canvas>

Rating:

<meter>

<dialog><time><m><menu><command><embed><figure><details>And many more…

New Semantics

<datagrid>

Page 81: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

<input type="datetime">

<input type="number"><input type="range"><input type="email"><input type="url">

And many more…

<input type="text" list="list"><datalist id="list"> <option value="Mr"> ...</datalist>

New Controls

Page 82: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Repetition Model

Add Team MemberAdd Team Member

Team Members

NameRank

Jack O'NeillJack O'Neill RemoveColonelColonel

RemoveSam CarterSam CarterMajorMajor

RemoveDaniel JacksonDaniel JacksonCivilianCivilian Remove

ContinueRemoveTeal’cTeal’cAlienAlien

<tr repeat-template="member" repeat="0"> <td><input type="text" name="member0.rank"></td> <td><input type="text" name="member0.name"></td> <td><button type="remove">Remove</button></td></tr><tr repeat-template="member" repeat="1"> <td><input type="text" name="member1.rank"></td> <td><input type="text" name="member1.name"></td> <td><button type="remove">Remove</button></td></tr><tr id="member" repeat="template" repeat-start="2"> <td><input type="text" name="member[member].rank"></td> <td><input type="text" name="member[member].name"></td> <td><button type="remove">Remove</button></td></tr>

<tr id="member" repeat="template" repeat-start="2"> <td><input type="text" name="member[member].rank"></td> <td><input type="text" name="member[member].name"></td> <td><button type="remove">Remove</button></td></tr>

<button type="add" template="member">Add Team Member</button>

Page 83: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Required Fields<input type="email" required="required">

Regular Expressions<input type="text" pattern="[A-Za-z0-9_\-]+">

Minimum and Maximum Values<input type="range" min="20" max="80">

MaxLength for textarea<textarea maxlength="2000"></textarea>

And many more…

Client-Side Form Validation

Page 84: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

XML Technologies and Applications

84

Page 85: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sample XML Data

85

<orders>

<order>

<onum>1020</onum>

<takenBy>1000</takenBy>

<customer>1111</customer>

<recDate>10-DEC 94</recDate>

<items>

<item>

<pnum>10506</pnum>

<quantity>1</quantity>

</item>

<item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items></order>...</orders>

Page 86: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sample Data

<orders>

<order>

<onum>1020</onum>

<takenBy>1000</takenBy>

<customer>1111</customer>

<recDate>10-DEC 94</recDate>

<items>

<item>

<pnum>10506</pnum>

<quantity>1</quantity>

</item>

<item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items></order>...</orders>

startDocument

endDocument

Parsing Event

Page 87: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sample Data

<orders>

<order>

<onum>1020</onum>

<takenBy>1000</takenBy>

<customer>1111</customer>

<recDate>10-DEC-94</recDate>

<items>

<item>

<pnum>10506</pnum>

<quantity>1</quantity>

</item>

<item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items></order>...</orders>

startElement

endElement

Page 88: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Sample Data

<orders>

<order>

<onum>1020</onum>

<takenBy>1000</takenBy>

<customer>1111</customer>

<recDate>10-DEC-94</recDate>

<items>

<item>

<pnum>10506</pnum>

<quantity>1</quantity>

</item>

<item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items></order>...</orders>

characters

Page 89: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

XML-Parsing Standards

89

Two parsing methods that implement W3C standards for accessing XML

SAX (Simple API for XML)

• event-driven parsing

• “serial access” protocol

• Read only API

DOM (Document Object Model)

• convert XML into a tree of objects

• “random access” protocol

• Can update XML document (insert/delete nodes)

Page 90: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

SAX Parsers

SAX Parser

When you see the start of the document do …

When you see the start of an element do … When you see

the end of an element do …

<?xml version="1.0"?>...

Page 91: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

The DOM Tree

91

Page 92: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Using a DOM Tree

92

DOM Parser DOM TreeXML File

API

Application

Page 93: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

Node Navigation

93

getFirstChild()

getPreviousSibling()

getChildNodes()

getNextSibling()

getLastChild()

getParentNode()

Page 94: Advanced Programming Rabie A. Ramadan Lecture 3. Network Programming An Overview 2.

SAX vs DOM Parsing: Efficiency

94

The DOM object built by DOM parsers is usually complicated and requires more memory storage than the XML file itself

A lot of time is spent on construction before use

For some very large documents, this may be impractical

SAX parsers store only local information that is encountered during the serial traversal

Hence, programming with SAX parsers is, in general, more efficient