topic 05 : web based protocols

Post on 18-Jan-2015

403 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Topic 5 : Web Based Protocols

Er. Pradip Kharbuja

Learning Objectives

• Define and use XML documents

• Understand and use the RSS protocol

• Incorporate XML parsing into PHP

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.

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

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

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.

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>

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.

XML Elements

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

• This may consist of:

- Other elements

- Attributes

- Text

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.

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>

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

The XML Tree Structure

<root>

<child>

<subchild>something</subchild>

</child>

<child>

<subchild>something</subchild>

</child>

</root>

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.

Types of XML

1. Well Formed XML

2. Valid XML

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.

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.

Datatypes of DTD elements

1. #PCDATA Parsed Character Data

2. #EMPTY

3. #ANY

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)>

]>

Example of DTD [Contd.]

<addresses>

<person>

<firstname>Michael</firstname>

<surname>Heron</surname>

<organisation>NCC</organisation>

</person>

</addresses>

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

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)>

]>

Types of DTD

1. Internal DTD

2. External DTD

External DTD

• save the rules in .dtd file

• Replace <!DOCTYPE....

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

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

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.

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.

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 />";

}

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.");

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");

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().

Our Architecture So Far

PHP

HTML

MySQL

Presentation

Application

Data XML

XML fits into the data layer of our architecture.

To Study yourself...

• Why use XML?

• Why not to use XML?

• Importance of Formats

• Importance of Protocols

• SMTP Protocol

• Internet Protocols and Formats

End of Topic 5

top related