topic 05 : web based protocols

34
Topic 5 : Web Based Protocols Er. Pradip Kharbuja

Upload: pradip-kharbuja

Post on 18-Jan-2015

402 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Topic 05 : Web Based Protocols

Topic 5 : Web Based Protocols

Er. Pradip Kharbuja

Page 2: Topic 05 : Web Based Protocols

Learning Objectives

• Define and use XML documents

• Understand and use the RSS protocol

• Incorporate XML parsing into PHP

Page 3: Topic 05 : Web Based Protocols

Introduction

• In this lecture, we are going to broaden our understanding of internet based

formats and protocols.

- This will be important for discussing web services later in the module.

• The internet is a complex system of platforms and user requirements.

- It is also a system that should be system agnostic.

• We do not know or care what kind of computers are receiving our

data.

Page 4: Topic 05 : Web Based Protocols

XML

• eXtensible Markup Language

• It is a small database.

• The file format is generally in .xml

• xml is case sensitive

• XML tags are not pre-defined.

• primary function of XML is to carry data

• main reason to use xml is portability

Page 5: Topic 05 : Web Based Protocols

XML

• XML is a very common data format on the internet, and is the basis for

many other protocols and formats.

- It stores data alongside the information needed to interpret that data

• That is often referred to as metadata – data about data

• A firm understanding of XML is required in order to understand how other

common formats work.

- Such as XHTML, SOAP and RSS

Page 6: Topic 05 : Web Based Protocols

XML

• XML (eXtensible Markup Language) is structured very much like HTML.

• The main difference is the tags that are used have no intrinsic meaning.

• Tags are user defined.

• It is a textual format (like HTML), which means that it is built in itself on the

unicode format.

• This makes it very simple to transmit across networks, as text data is a

well understood quantity.

Page 7: Topic 05 : Web Based Protocols

A Simple XML Document

• An XML document begins by providing versioning information at the start.

• It is then followed by one root element or document element.

• It is then followed by child elements.<?xml version="1.0"?>

<addresses>

<person>

<firstname>Michael</firstname>

<surname>Heron</surname>

<organisation>NCC</organisation>

</person>

</addresses>

Page 8: Topic 05 : Web Based Protocols

Rules of XML

• XML is case sensitive.

• Every tag must be closed by the matching end tag. In case of empty tag, / is used.

eg. <person></person>, <br/>

• XML declaration must be present in every xml file.

• There should not be any space or character before XML declaration

• Value of every attribute must be enclosed by single or double quote.

• There must be only one root element in an XML.

Page 9: Topic 05 : Web Based Protocols

XML Elements

• An XML element is anything that is contained within starting and closing tags.

• This may consist of:

- Other elements

- Attributes

- Text

Page 10: Topic 05 : Web Based Protocols

XML Attributes

• Attributes allow us to specify additional information about elements.

- In HTML, we have the <a> tag, which indicates a hyperlink. We provide

it the destination with an attribute href:

• <a href = "http://www.google.com">

• XML permits us to do the same thing in our elements.

Page 11: Topic 05 : Web Based Protocols

A Simple XML Document with Attributes

<?xml version="1.0"?>

<addresses>

<person gender="male">

<firstname>Michael</firstname>

<surname>Heron</surname>

<organisation>NCC</organisation>

<notes>Don't buy a used car from this man.</notes>

</person>

</addresses>

Page 12: Topic 05 : Web Based Protocols

The XML Tree Structure

• All of the elements within an XML document must confirm to a tree structure.

- The document has a root element, which has one or more child elements.

• Each of these child elements may have one or more subchild elements.

-And so on

Page 13: Topic 05 : Web Based Protocols

The XML Tree Structure

<root>

<child>

<subchild>something</subchild>

</child>

<child>

<subchild>something</subchild>

</child>

</root>

Page 14: Topic 05 : Web Based Protocols

Task

1. Design an XML data page to be used for an address book supporting

multiple email ids and contact numbers.

2. Design an XML page to store the course details including their available

semesters and their subjects.

Page 15: Topic 05 : Web Based Protocols

Types of XML

1. Well Formed XML

2. Valid XML

Page 16: Topic 05 : Web Based Protocols

Well Formed XML

• XML has a particular syntax it must follow.

- Rules about how names and elements are used.

• That is the XML Format

• XML documents that consists of these XML rules are called Well Formed XML.

Page 17: Topic 05 : Web Based Protocols

Valid XML

• Valid XML is a well formed XML document that also conforms to a set of

typing conventions known as a Document Type Definition (DTD).

- This DTD defines what is ‘legal’ XML for a particular kind of element.

• All valid XML is always well formed XML but all well formed XML may not

be valid XML.

• An XML Schema can also be used for the same purpose.

- To provide for type checking and to provide a platform independent

method of interpreting XML Data.

Page 18: Topic 05 : Web Based Protocols

Datatypes of DTD elements

1. #PCDATA Parsed Character Data

2. #EMPTY

3. #ANY

Page 19: Topic 05 : Web Based Protocols

Example of DTD

<?xml version="1.0"?>

<!DOCTYPE addresses[

<!ELEMENT addresses (person)>

<!ELEMENT person (firstname, surname, organisation) >

<!ELEMENT firstname (#PCDATA)>

<!ELEMENT surname (#PCDATA)>

<!ELEMENT organisation (#PCDATA)>

]>

Page 20: Topic 05 : Web Based Protocols

Example of DTD [Contd.]

<addresses>

<person>

<firstname>Michael</firstname>

<surname>Heron</surname>

<organisation>NCC</organisation>

</person>

</addresses>

Page 21: Topic 05 : Web Based Protocols

Access Modifier of DTD

1. + one or more occurances

2. * zero or more occurances

3. ? zero or one occurance

4. if nothing written, only one time and is required

Page 22: Topic 05 : Web Based Protocols

Example of DTD with Access Modifier

<?xml version="1.0"?>

<!DOCTYPE addresses[

<!ELEMENT addresses (person*)>

<!ELEMENT person (firstname, surname?, organisation+) >

<!ELEMENT firstname (#PCDATA)>

<!ELEMENT surname (#PCDATA)>

<!ELEMENT organisation (#PCDATA)>

]>

Page 23: Topic 05 : Web Based Protocols

Types of DTD

1. Internal DTD

2. External DTD

Page 24: Topic 05 : Web Based Protocols

External DTD

• save the rules in .dtd file

• Replace <!DOCTYPE....

with <!DOCTYPE root_element SYSTEM "filename.dtd">

Page 25: Topic 05 : Web Based Protocols

RSS

• RSS (Really Simple Syndication).

• RSS is an XML based format for providing updates on changed or new

content within a website.

• Blogs, news sites and other such web pages will usually publish an RSS feed.

• e.g. http://feeds.bbci.co.uk/news/rss.xml

• e.g. http://weather.yahooapis.com/forecastrss?w=2269179

Page 26: Topic 05 : Web Based Protocols

RSS

• Other web enabled applications can then use this feed in order to update

users with changing content on other sites.

• The RSS standard defines what tag names are used to transport information.

• These include any images, links to the main story, summaries, languages,

copyright reasons, and so on.

- These can then be incorporated into an application in whatever way the

developer desires.

Page 27: Topic 05 : Web Based Protocols

Parsing XML with PHP

• PHP comes complete with an XML parsing system.

• We need functions to deal with:

- Encountering a start tag

- Encountering an end tag

- Encountering content

- These are the callback functions.

Page 28: Topic 05 : Web Based Protocols

Simple XML Parsing

function contents($parser, $data){

echo $data;

}

function startTag($parser, $data){

echo "&lt; $data &gt;";

}

function endTag($parser, $data){

echo "&lt; $data &gt; <br />";

}

Page 29: Topic 05 : Web Based Protocols

Setting up Parser

• To create a parser, we need an XML file to parse.

• We can open a remote internet file in PHP in the same way we can open a

local file.

- Through the fopen() function.

• We create the parser itself by using xml_parser_create().

- $xml_parser = xml_parser_create() or die("Couldn create parser.");

Page 30: Topic 05 : Web Based Protocols

Setting up Parser [Contd.]

• We need to set the functions responsible for finding start tags and end tags:

- xml_set_element_handler($xml_parser, "startTag", "endTag");

• And the function for content:

- xml_set_character_data_handler($xml_parser, "contents");

Page 31: Topic 05 : Web Based Protocols

Reading and Parsing the File

• As the file is a remote file, we need to read from the server in chunks.

- This is done using the fread() function.

• We take those chunks and then pass them through our parser.

- We do this through the xml_parse() function

- This then calls the callback functions where it encounters the tags & elements.

• At the end, we free the parser and close the file reference using fclose() and

xml_parser_free().

Page 32: Topic 05 : Web Based Protocols

Our Architecture So Far

PHP

HTML

MySQL

Presentation

Application

Data XML

XML fits into the data layer of our architecture.

Page 33: Topic 05 : Web Based Protocols

To Study yourself...

• Why use XML?

• Why not to use XML?

• Importance of Formats

• Importance of Protocols

• SMTP Protocol

• Internet Protocols and Formats

Page 34: Topic 05 : Web Based Protocols

End of Topic 5