Академический Документы
Профессиональный Документы
Культура Документы
INTERNET PROGRAMMING
1151CS202 2 0 2 3
Course Category: Program Core
A. Preamble:
This course Internet Programming provides an introduction and Basic Concepts of Server-Side
Programming and Designing of Static and Dynamic WebPages.
B. Pre-requisites:
Sl. No Course Code Course Name
1 1151CS103 Programming in Java
C. Related Courses:
Sl. No Course Code Course Name
1 1156CS601 Minor Project
D. Course Outcomes:
Upon the successful completion of the course, learners will be able to
Level of learning
CO domain (Based on
Course Outcomes
Nos. revised Bloom’s
taxonomy)
Demonstrate web applications using Hypertext Markup Language,
CO1 K2
Cascading style sheets and Java Database Connectivity.
Explain about the java networking technologies and working of struts K2
CO2
framework.
Develop applications using hibernate framework and Hypertext Markup
CO3 Language Protocols. K3
F. Course Content:
UNIT I WEBSITES BASICS, HTML 5, CSS 3 9
Basics-RIA Rich Internet Applications Internet technologies Overview –Understanding the
difference between internet and intranet; HTML and CSS: HTML 5.0, XHTML, CSS 3- TCP
and Datagram Sockets – Remote Method Invocation - Servlets, Java Server Pages. – JDBC Java
Beans – Enterprise Java Beans.
1. Deitel&Deitel, Goldberg, “Internet and world wide web – How to Program”, Pearson Education
Asia, 2011.
1. Chris Bates, Web Programming – Building Intranet Applications, 3 rd. Edition, Wiley Publications,
2009.
2. Jeffrey C and Jackson, “Web Technologies A Computer Science Perspective”, Pearson Education,
2011.
3. UttamK. Roy, “Web Technologies”, Oxford University Press, 2011.
1. Eric Ladd, Jim O’ Donnel, “Using HTML 4, XML and JAVA”, Prentice Hall of India –
QUE, 1999.
2. PHP &MYSQL TimConverse, Joyce Park, Alark Morgan.
3. PHP for Beginners VikramVaswani, Tata McGraw-Hill
4. Ajax in Action, Dreamtech press
5. WWW. W3schools.com
UNIT IV
PHP started out as a small open source project that evolved as more and more people found out
how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.
Characteristics of PHP
Five important characteristics make PHP's practical nature possible −
Simplicity
Efficiency
Security
Flexibility
Familiarity
As mentioned earlier, PHP is embedded in HTML. That means that in amongst your normal
HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this −
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo "Hello, World!";?>
</body>
</html>
Hello, World!
If you examine the HTML output of the above example, you'll notice that the PHP code is not
present in the file sent from the server to your Web browser. All of the PHP present in the Web
page is processed and stripped from the page; the only thing returned to the client from the Web
server is pure HTML output.
All PHP code must be included inside one of the three special markup tags ATE are recognised
by the PHP Parser.
A most common tag is the <?php...?> and we will also use the same tag in our tutorial.
From the next chapter we will start with PHP Environment Setup on your machine and then we
will dig out almost all concepts related to PHP to make you comfortable with the PHP language.
This chapter will give you an idea of very basic syntax of PHP and very important to make your
PHP foundation strong.
Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code from other elements in the page.
The mechanism for doing so is known as 'escaping to PHP'. There are four ways to do this −
<?php...?>
If you use this style, you can be positive that your tags will always be correctly interpreted.
<?...?>
Short tags are, as one might expect, the shortest option You must do one of two things to enable
PHP to recognize the tags −
ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style
tags look like this −
<%...%>
To use ASP-style tags, you will need to set the configuration option in your php.ini file.
Single-line comments − They are generally used for short explanations or notes relevant to the
local code. Here are the examples of single line comments.
<?
# This is a comment, and
# This is the second line of the comment
Multi-lines printing − Here are the examples to print multiple lines in a single print statement −
<?
# First Example
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
# Second Example
print "This spans
multiple lines. The newlines will be
output as well";
?>
Multi-lines comments − They are generally used to provide pseudocode algorithms and more
detailed explanations when necessary. The multiline style of commenting is the same as in C.
Here are the example of multi lines comments.
<?
/* This is a comment with multiline
Author : Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/
PHP whitespace insensitive means that it almost never matters how many whitespace characters
you have in a row.one whitespace character is the same as many such characters.
For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable
$four is equivalent −
Live Demo
<html>
<body>
<?php
$capital = 67;
print("Variable capital is $capital<br>");
print("Variable CaPiTaL is $CaPiTaL<br>");
?>
</body>
</html>
if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.<br>");
if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.<br>");
}
Live Demo
<?php
echo "Hello PHP!!!!!";
?>
$ php test.php
It will produce the following result −
Hello PHP!!!!!
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
With PHP you are not limited to output HTML. You can output images, PDF files, and even
Flash movies. You can also output any text, such as XHTML and XML.
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side
What's new in PHP 7
PHP 7 is much faster than the previous popular stable release (PHP 5.6)
PHP 7 has improved Error Handling
PHP 7 supports stricter Type Declarations for function arguments
PHP 7 supports new operators (like the spaceship operator: <=> )
Using PHP
.phtml
.php3
.php4
.php5
.phps
PHP was designed to work with HTML, and as such, it can be embedded into the HTML code.
You can create PHP files without any html tags and that is called Pure PHP file .
The server interprets the PHP code and outputs the results as HTML code to the web browsers.
In order for the server to identify the PHP code from the HTML code, we must always enclose
the PHP code in PHP tags.
A PHP tag starts with the less than symbol followed by the question mark and then the words
“php”.
The PHP tags themselves are not case-sensitive, but it is strongly recommended that we use
lower case letter. The code below illustrates the above point.
<?php … ?>
We will be referring to the PHP lines of code as statements. PHP statements end with a semi
colon (;). If you only have one statement, you can omit the semi colon. If you have more than
one statement, then you must end each line with a semi colon. For the sake of consistency, it is
recommended that you always end your statement(s) with a semi colon. PHP scripts are
executed on the server. The output is returned in form of HTML.
<?php
echo "Hello world";
?>
Output:
Hello world
Variables
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
Output:-
Hello world!
5
10.5
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
</body>
</html>
Output:-
I love W3Schools.com!
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
</body>
</html>
Output:-
9
In PHP 7, type declarations were added. This gives an option to specify the data type expected
when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on
a type mismatch.
You will learn more about strict and non-strict requirements, and data type declarations in
the PHP Functions chapter.
The scope of a variable is the part of the script where the variable can be referenced/used.
local
global
static
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
Output:-
Variable x inside function is:
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>
Output:-
Variable x inside function is: 5
To do this, use the global keyword before the variables (inside the function):
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$y = $x + $y;
?>
</body>
</html>
Output:-
15
To do this, use the static keyword when you first declare the variable:
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
Output:-
0
1
2
Program control
Sequential – this one involves executing all the codes in the order in which they have been
written.
Decision – this one involves making a choice given a number of options. The code executed
depends on the value of the condition.
A control structure is a block of code that decides the execution path of a program depending on
the value of the set condition.
Let’s now look at some of the control structures that PHP supports.
PHP IF Else
If… then... else is the simplest control structure. It evaluates the conditions using Boolean logic
When to use if… then… else
You have a block of code that should be executed only if a certain condition is true
You have two options, and you have to select one.
If… then… else if… is used when you have to select more than two options and you have to
select one or more
<?php
if (condition is true) {
block one
else
block two
}
?>
HERE,
How it works The flow chart shown below illustrates how the if then… else control structure
works
Let’s see this in action The code below uses “if… then… else” to determine the larger value
between two numbers.
<?php
$first_number = 7;
$second_number = 21;
}else{
?>
Output:
21 is greater than 7
If no condition has been met then the default block of code is executed.
<?php
switch(condition){
case value:
break;
case value2:
break;
default:
break;
}
?>
HERE,
How it works
The flow chart shown below illustrates how the switch control structure works
Practical example
The code below uses the switch control structure to display a message depending on the day of
the week.
<?php
$today = "wednesday";
switch($today){
case "sunday":
break;
case "wednesday":
case "saturday":
break;
default:
break;
?>
Output:
A Loop is an Iterative Control Structure that involves executing the same number of code a
number of times until a certain condition is met.
The above code outputs “21 is greater than 7” For loops For... loops execute the block of code a
specifiednumber of times. There are basically two types of for loops;
for
for… each.
Let’s now look at them separately. For loop It has the following basic syntax
<?php
for (initialize; condition; increment){
//code to be executed
}
?>
HERE,
How it works
The flowchart shown below illustrates how for loop in php works
How to code
The code below uses the “for… loop” to print values of multiplying 10 by 0 through to 10
<?php
$product = 10 * $i;
?>
Output:
The product of 10 x 0 is 0
The product of 10 x 1 is 10
The product of 10 x 2 is 20
The product of 10 x 3 is 30
The product of 10 x 4 is 40
The product of 10 x 5 is 50
The product of 10 x 6 is 60
The product of 10 x 7 is 70
The product of 10 x 8 is 80
The product of 10 x 9 is 90
The php foreach loop is used to iterate through array values. It has the following basic syntax
<?php
foreach($array_variable as $array_values){
}
?>
HERE,
How it works The flowchart shown below illustrates how the for… each… loop works
Practical examples
The code below uses for… each loop to read and print the elements of an array.
<?php
$animals_list = array("Lion","Wolf","Dog","Leopard","Tiger");
foreach($animals_list as $array_values){
?>
Output:
Lion
Wolf
Dog
Leopard
Tiger
<?php
?>
The names have been used as array keys and gender as the values.
Output:
Mary is Female
John is Male
Mirriam is Female
While Loop
PHP While loop
They are used to execute a block of code a repeatedly until the set condition gets satisfied
While loops are used to execute a block of code until a certain condition becomes true.
You can use a while loop to read records returned from a database query.
Do… while - executes the block of code at least once before evaluating the condition
While… - checks the condition first. If it evaluates to true, the block of code is executed as long
as the condition is true. If it evaluates to false, the execution of the while loop is terminated.
While loop
It has the following syntax
<?php
while (condition){
}
?>
HERE,
How it works
The flow chart shown below illustrates how the while… loop works
Practical example
<?php
$i = 0;
echo $i + 1 . "<br>";
$i++;
?>
Output:
1
2
3
4
5
PHP Do While
The difference between While… loop and Do… while loop is do… while is executed at-least
once before the condition is evaluated.
<?php
do{
}
?>
while(condition);
HERE,
How it works
The flow chart shown below illustrates how the while… loop works
Practical example
We are now going to modify the while… loop example and implement it using the do… while
loop and set the counter initial value to 9.
<?php
$i = 9;
do{
?>
Summary
The for… loop is used to execute a block of a specified number of times
The foreach… loop is used to loop through arrays
While… loop is used to execute a block of code as long as the set condition is made to be false
The do… while loop is used to execute the block of code at least once then the rest of the
execution is dependent on the evaluation of the set condition
Built-in Functions
What is a Function?
A function is a reusable piece or block of code that performs a specific action.
Functions can either return values when called or can simply perform an operation without
returning any value.
PHP has over 700 functions built in that perform different tasks.
Built in Functions
Built in functions are functions that exist in PHP installation package.
These built in functions are what make PHP a very efficient and productive scripting language.
The built in functions can be classified into many categories. Below is the list of the categories.
String Functions
These are functions that manipulate string data, refer to the article on strings for implementation
examples of string functions
Numeric Functions
Numeric functions are function that return numeric results.
Numeric php function can be used to format numbers, return constants, perform mathematical
computations etc.
What is a Function?
A function is a reusable piece or block of code that performs a specific action.
Functions can either return values when called or can simply perform an operation without
returning any value.
PHP has over 700 functions built in that perform different tasks.
Built in Functions
Built in functions are functions that exist in PHP installation package.
These built in functions are what make PHP a very efficient and productive scripting language.
The built in functions can be classified into many categories. Below is the list of the categories.
String Functions
These are functions that manipulate string data, refer to the article on strings for implementation
examples of string functions
Numeric Functions
Numeric functions are function that return numeric results.
Numeric php function can be used to format numbers, return constants, perform mathematical
computations etc.
<?php true
if(is_numeric (123))
{
echo "true";
}
else
{
echo "false";
}
?>
number_format Used to formats a numeric value <?php 2,509,663
echo
using digit separators and decimal number_format(2509663);
points ?>
Function Description Example Output
Date Function
The date function is used to format Unix date and time to human readable format.
Other functions
These include;
you have routine tasks in your application such as adding data to the database
performing validation checks on the data
Authenticating users in the system etc.
Creating a function that all these pages can be calling is one of the features that make PHP a
powerful scripting language.
Before we create our first user defined function, let’s look at the rules that we must follow when
creating our own functions.
Function names must start with a letter or an underscore but not a number
The function name must be unique
The function name must not contain spaces
It is considered a good practice to use descriptive function names.
Functions can optionally accept parameters and return values too.
Let’s now create our first function. We will create a very basic function that illustrates the major
components of a function in PHP.
<?php
function add_numbers(){
echo 1 + 2;
}
add_numbers ();
?>
Output:
HERE,
“function…(){…}” is the function block that tells PHP that you are defining a custom function
“add_numbers” is the function name that will be called when using the function.
“()” can be used to pass parameters to the function.
“echo 'Hello function!';” is the function block of code that is executed. It could be any code
other than the one used in the above example.
Let’s now look at a fairly complex example that accepts a parameter and display a message just
like the above function.
Suppose we want to write a function that prints the user name on the screen, we can write a
custom function that accepts the user name and displays it on the screen.
<?php
function display_name($name)
{
echo "Hello " . $name;
}
display_name("Martin Luther King");
?>
Output:
HERE,
Let’s now look at a function that accepts a parameter and then returns a value. We will create a
function that converts kilometers to miles. The kilometers will be passed as a parameter. The
function will return the miles equivalent to the passed kilometers. The code below shows the
implementation.
<?php
function kilometers_to_miles($kilometers = 0)
{
$miles_scale = 0.62;
return $kilometers * $miles_scale;
}
echo kilometers_to_miles(100);
?>
Output:
62
Connecting to Database
PHP 5 and later can work with a MySQL database using:
PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL
databases.
So, if you have to switch your project to use another database, PDO makes the process easy. You
only have to change the connection string and a few queries. With MySQLi, you will need to
rewrite the entire code - queries included.
Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very
important for web application security.
MySQLi (object-oriented)
MySQLi (procedural)
PDO
MySQLi Installation
For Linux and Windows: The MySQLi extension is automatically installed in most cases, when
php5 mysql package is installed.
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
$connect_error was broken until PHP 5.2.9 and 5.3.0. If you need to ensure compatibility with
PHP versions prior to 5.2.9 and 5.3.0, use the following code instead:
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Example (PDO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Note: In the PDO example above we have also specified a database (myDB). PDO require a
valid database to connect to. If no database is specified, an exception is thrown.
Tip: A great benefit of PDO is that it has an exception class to handle any problems that may
occur in our database queries. If an exception is thrown within the try{ } block, the script stops
executing and flows directly to the first catch(){ } block.
Close the Connection
The connection will be closed automatically when the script ends. To close the connection
before, use the following:
MySQLi Object-Oriented:
$conn->close();
MySQLi Procedural:
mysqli_close($conn);
PDO:
$conn = null;
Using Cookies
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the
user's computer. Each time the same computer requests a page with a browser, it will send the
cookie too. With PHP, you can both create and retrieve cookie values.
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Only the name parameter is required. All other parameters are optional.
Example
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Note: The setcookie() function must appear BEFORE the <html> tag.
Note: The value of the cookie is automatically URLencoded when sending the cookie, and
automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
Example
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the past:
Example
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
Regular Expressions
Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions.
Regular expressions help you accomplish tasks such as validating email addresses, IP address
etc.
Regular expressions simplify identifying patterns in string data by calling a single function. This
saves us coding time.
When validating user input such as email address, domain names, telephone numbers, IP
addresses,
Highlighting keywords in search results
When creating a custom HTML template. Regular expressions can be used to identify the
template tags and replace them with actual data.
preg_match – this function is used to perform a pattern match on a string. It returns true if a
match is found and false if a match is not found.
preg_split – this function is used to perform a pattern match on a string and then split the
results into a numeric array
preg_replace – this function is used to perform a pattern match on a string and then replace the
match with the specified text.
<?php
function_name('/pattern/',subject);
?>
HERE,
Let’s now look at practical examples that implement the above regular expression functions in
PHP.
PHP Preg_match
The first example uses the preg_match function to perform a simple pattern match for the word
guru in a given URL.
The code below shows the implementation for the above example.
<?php
$my_url = "www.guru99.com";
if (preg_match("/guru/", $my_url))
{
echo "the url $my_url contains guru";
}
else
{
echo "the url $my_url does not contain guru";
}
?>
Let’s examine the part of the code responsible for our output "preg_match('/guru/',
$my_url)" HERE,
PHP Preg_split
Let’s now look at another example that uses the preg_split function.
We will take a string phrase and explode it into an array; the pattern to be matched is a single
space.
The text string to be used in this example is "I Love Regular Expressions".
<?php
?>
PHP Preg_replace
Let’s now look at the preg_replace function that performs a pattern match and then replaces the
pattern with something else.
It replaces the word guru with the word guru surrounded by css code that highlights the
background colour.
<?php
echo $text;
?>
Assuming you have saved the file preg_replace.php, browser to the URL
http://localhost/phptuts/preg_replace.php
Meta characters
The above examples used very basic patterns; metacharacters simply allow us to perform more
complex pattern matches such as test the validity of an email address. Let’s now look at the
commonly used metacharacters.
Matches any single character except a new /./ matches anything that has a single
.
line character
Matches the beginning of or string / excludes /^PH/ matches any string that starts
^
characters with PH
a-z Matches lower case letters /a-z/ matches cool, happy etc.
A-Z Matches upper case letters /A-Z/ matches WHAT, HOW, WHY etc.
The above list only gives the most commonly used metacharacters in regular expressions.
Let’s now look at a fairly complex example that checks the validity of an email address.
<?php
$my_email = "name@company.com
";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/",
$my_email)) {
echo "$my_email is a valid email address";
}
else
{
echo "$my_email is NOT a valid email address";
}
?>
filter_none
brightness_4
<!DOCTYPE html>
<html>
<h1>Note</h1>
<body>
<p>To:RAJ
<br>
From:RAVI
</p>
<h1>Reminder</h1>
<p>Meeting at 8am</p>
</body>
</html>
OUTPUT :
Note : The output in both the cases is same but while using HTML we have used predefined tags
like p tag and h1 tag whereas while using XML we have used self defined tags like “To” tag and
“from” tag.
Another Example :
The XML above is quite self-descriptive:
Input :
Output :
Basically XML above does not do anything. XML is just information wrapped in tags. User must
require a piece of software to send, receive, store, or display it.
XML makes web development User Friendly : Many computer systems contain data in
incompatible formats. Exchanging data between incompatible systems or upgraded systems is a
time-consuming task for web developers. Large amounts of data must be converted, and
incompatible data is often lost. XML stores data in plain text format. This provides a software-
and hardware-independent way of storing, transporting, and sharing data.
XML is Extensible : XML applications will work as expected even if data is edited i.e. added or
removed. Example :The above note is edited into a newer version by adding date tag and hour
tag , and by removing the heading tag.
Previous version
New Version
The XML Schema language is also referred to as XML Schema Definition (XSD).
XSD Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The purpose of an XML Schema is to define the legal building blocks of an XML document:
With XML Schemas, the sender can describe the data in a way that the receiver will understand.
A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other
countries as 11.March.
<date type="date">2004-03-11</date>
ensures a mutual understanding of the content, because the XML data type "date" requires the
format "YYYY-MM-DD".
Even if documents are well-formed they can still contain errors, and those errors can have
serious consequences.
Think of the following situation: you order 5 gross of laser printers, instead of 5 laser printers.
With XML Schemas, most of these errors can be caught by your validating software.
What is a DTD?
A DTD is a Document Type Definition.
A DTD defines the structure and the legal elements and attributes of an XML document.
Output:-
Tove
Jani
Reminder
Don't forget me this weekend!
The DTD above is interpreted like this:
!DOCTYPE note defines that the root element of this document is note
!ELEMENT note defines that the note element must contain four elements:
"to,from,heading,body"
!ELEMENT to defines the to element to be of type "#PCDATA"
!ELEMENT from defines the from element to be of type "#PCDATA"
!ELEMENT heading defines the heading element to be of type "#PCDATA"
!ELEMENT body defines the body element to be of type "#PCDATA"
Output:-
Tove
Jani
Reminder
Don't forget me this weekend!
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and style
of a document."
The HTML DOM defines a standard way for accessing and manipulating HTML documents. It
presents an HTML document as a tree-structure.
The XML DOM defines a standard way for accessing and manipulating XML documents. It
presents an XML document as a tree-structure.
Understanding the DOM is a must for anyone working with HTML or XML.
Example
<!DOCTYPE html>
<html>
<body>
<p>This is a paragraph.</p>
<script>
</script>
</body>
</html>
Output:-
Hello World!
This is a paragraph.
In other words: The XML DOM is a standard for how to get, change, add, or delete XML
elements.
XML Parsers and Validation
Non DTD XML files must use the predefined character entities for amp(&), apos(single
quote), gt(>), lt(<), quot(double quote).
It must follow the ordering of the tag. i.e., the inner tag must be closed before closing the
outer tag.
Each of its opening tags must have a closing tag or it must be a self ending
tag.(<title>....</title> or <title/>).
It must have only one attribute in a start tag, which needs to be quoted.
amp(&), apos(single quote), gt(>), lt(<), quot(double quote) entities other than these
must be declared.
Example
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
It defines the type of document. Here, the document type is element type.
It includes a root element named as address.
Each of the child elements among name, company and phone is enclosed in its self
explanatory tag.
Order of the tags is maintained.
XML parser is a software library or a package that provides interface for client
applications to work with XML documents. It checks for proper format of the XML document
and may also validate the XML documents. Modern day browsers have built-in XML parsers.
Following diagram shows how XML parser interacts with XML document −
To ease the process of parsing, some commercial products are available that facilitate the
breakdown of XML document and yield more reliable results.
MSXML (Microsoft Core XML Services) − This is a standard set of XML tools from
Microsoft that includes a parser.
System.Xml.XmlDocument − This class is part of .NET library, which contains a
number of different classes related to working with XML.
Java built-in parser − The Java library has its own parser. The library is designed such
that you can replace the built-in parser with an external implementation such as Xerces
from Apache or Saxon.
Saxon − Saxon offers tools for parsing, transforming, and querying XML.
Xerces − Xerces is implemented in Java and is developed by the famous open source
Apache Software Foundation.
In case of HTML document, tags are predefined such as table, div, and span; and the browser
knows how to add style to them and display those using CSS styles. But in case of XML
documents, tags are not predefined. In order to understand and style an XML document, World
Wide Web Consortium (W3C) developed XSL which can act as XML based Stylesheet
Language. An XSL document specifies how a browser should render an XML document.
XSLT − used to transform XML document into various other types of document.
XPath − used to navigate XML document.
XSL-FO − used to format XML document.
What is XSLT
XSLT, Extensible Stylesheet Language Transformations, provides the ability to transform XML
data from one format to another automatically.
An XSLT stylesheet is used to define the transformation rules to be applied on the target XML
document. XSLT stylesheet is written in XML format. XSLT Processor takes the XSLT
stylesheet and applies the transformation rules on the target XML document and then it generates
a formatted document in the form of XML, HTML, or text format. This formatted document is
then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-
user.
Advantages
Any time you want to retrieve the latest headlines from your favorite sites, you can access the
available RSS Feeds via a desktop RSS reader. You can also make an RSS Feed for your own
site if your content changes frequently.
In brief:
RSS is a protocol that provides an open method of syndicating and aggregating web
content.
RSS is a standard for publishing regular updates to web-based content.
RSS is a Syndication Standard based on a type of XML file that resides on an Internet
server.
RSS is an XML application, which conforms to the W3C's RDF specification and is
extensible via XML.
You can also download RSS Feeds from other sites to display the updated news items on
your site, or use a desktop or online reader to access your favorite RSS Feeds.
What does RSS stand for? It depends on what version of RSS you are using.
A website willing to publish its content using RSS creates one RSS Feed and keeps it on
a web server. RSS Feeds can be created manually or with software.
A website visitor will subscribe to read your RSS Feed. An RSS Feed will be read by an
RSS Feed reader.
The RSS Feed Reader reads the RSS Feed file and displays it. The RSS Reader displays
only new items from the RSS Feed.
The RSS Feed reader can be customized to show you content related to one or more RSS
Feeds and based on your own interest.
New Homes - Realtors can provide updated Feeds of new home listings on the market.
Job Openings - Placement firms and newspapers can provide a classified Feed of job
vacancies.
Auction Items - Auction vendors can provide Feeds containing items that have been
recently added to eBay or other auction sites.
Press Distribution - Listing of new releases.
Schools - Schools can relay homework assignments and quickly announce school
cancellations.
News & Announcements - Headlines, notices, and any list of announcements.
Entertainment - Listings of the latest TV programs or movies at local theatres.
RSS is growing in popularity. The reason is fairly simple. RSS is a free and easy way to promote
a site and its content without the need to advertise or create complicated content sharing
partnerships.
RSS - Advantages
RSS is taking off so quickly because people are liking it. RSS is easy to use and it has
advantages for a publisher as well as for a subscriber. Here we have listed out a few advantages
of RSS for subscribers as well as for publishers.
All news at one place: You can subscribe to multiple news groups and then you can
customize your reader to have all the news on a single page. It will save you a lot of time.
News when you want it: Rather than waiting for an e-mail, you go to your RSS reader
when you want to read a news. Furthermore, RSS Feeds display more quickly than
information on web-sites, and you can read them offline if you prefer.
Get the news you want: RSS Feed comes in the form of headlines and a brief
description so that you can easily scan the headlines and click only those stories that
interest you.
Freedom from e-mail overload: You are not going to get any email for any news or
blog update. You just go to your reader and you will find updated news or blog
automatically whenever there is a change on the RSS server.
Easy republishing: You may be both a subscriber and a publisher. For example, you
may have a web-site that collects news from various other sites and then republishes it.
RSS allows you to easily capture that news and display it on your site.
Here are some of the advantages of RSS if you publish on the Web:
Easier publishing: RSS is really simple publishing. You don't have to maintain a
database of subscribers to send your information to them, instead they will access your
Feed using a reader and will get updated content automatically.
A simpler writing process: If you have a new content on your web site, you only need to
write an RSS Feed in the form of titles and short descriptions, and link back to your site.
An improved relationship with your subscribers: Because people subscribe from their
side, they don't feel as if you are pushing your content on them.
The assurance of reaching your subscribers: RSS is not subject to spam filters, your
subscribers get the Feeds, which they subscribe to and nothing more.
Links back to your site: RSS Feeds always include links back to a website. It directs a
lot of traffic towards your website.
Relevance and timeliness: Your subscribers always have the latest information from
your site.
RSS was first invented by Netscape. They wanted to use an XML format to distribute news,
stories, and information. Netscape refined the version of RSS and then dropped it.
Later Userland Software started controlling RSS specifications and releasing newer RSS
versions. They continued development of their own version of RSS and eventually UserLand
released RSS v2.
We will see Feed formats for these versions in the next chapter.
RSS has been released in many different versions in the last 10 years. Here we will give you
detail about three most commonly used RSS version.
Many sites offer RSS Feeds, which you can identify by a small yellow button that says either
or . However, if you click one of these links, you will most likely get a page full of
code in your browser.
To properly read the Feed, you need an RSS reader. Here are the steps to get and use RSS Feed
readers.
NewsGator Online - A free online RSS reader. Includes synchronization with Outlook,
viewing TV content with Media Center Edition, and publication of blogs and headlines.
RssReader - A free Windows-based RSS reader. Supports RSS versions 0.9x, 1.0, and
2.0, and Atom 0.1, 0.2, and 0.3.
FeedDemon - A Windows-based RSS reader. Very easy to use and has a very orderly
interface. However, this is not freeware!
blogbot - An RSS reader plug-in for Outlook or Internet Explorer. The light-version for
Internet Explorer is free.
When you first launch a standalone reader, most often, you will see a toolbar and three window
panes arranged much like the preview mode in Microsoft Outlook. The pane on the left side
typically displays the RSS Feeds, or channels, to which you are subscribed. These can be
organized into categories or folders.
The upper-right panel typically shows a list of articles within whichever channel is selected, and
the article content is then displayed in the lower-right panel. To change channel groups, just click
the drop-down box at the upper left beneath the menus. Sometimes a brief description will
appear in the lower right; if so, click the link in the article to load the complete text.
Some standalone apps can be configured to send you e-mail every time there's a new article on a
topic you're interested in.
Now go back to your newsreader, choose the category where you want the new subscription to
live (Business, Entertainment, the New York Times), and select New or New Channel from the
File menu. In most cases, the URL you copied should automatically be pasted into the URL field
in the New Channel wizard. If not, you can cut and paste the URL yourself.
A Filter tool will show only articles that contain a keyword you specify. This may also be labeled
Search. To use it, type a keyword directly into the Filter/Search bar.
Some readers include the ability to set a watch, an automatic search through all your incoming
Feeds for a specific keyword. For example, you could enter ICQ as a watch. If any article in any
Feed you subscribe to mentions ICQ, the article will be included in the Watch list.
You need to check the help section of your reader to find out more options to customize it
according to your needs.
Now you are aware how to write an RSS Feed for your site. If you don't know how to prepare
RSS Feed file, then please go through the RSS Feed Formats chapter.
First decide which version of RSS Feed you are going to use for your site. We would
recommend you to use the latest version available.
Create your RSS Feed in a text file with extension either .xml or .rdf. Upload this file on
your web server.
You should validate your RSS Feed before making it live. Check the next chapter on RSS
Feed Validation.
Create a link on your Web Pages for the RSS Feed file. You will use a small yellow
button for the link that says either or .
Now, your RSS Feed is online and people can start using it. But there are ways to promote your
RSS Feed so that more number of people can use your RSS Feed.
So now, you have got RSS Feed buttons from Google, Yahoo, and MSN. You must make sure to
update your content frequently and that your RSS Feed is constantly available.
If you have created one RSS Feed for your news group or web blog or for any other purpose,
then it is your responsibility to ensure that your RSS Feed file can be parsed by the XML parser
of any subscribing site.
Many of the RSS Feed creation softwares validate XML at the time of Feed creation but some
don't. Make a note that small errors can make your Feed unreadable by the standard Feed
readers.
So we would suggest you to make sure you have done all the required validations before
publishing your RSS Feed. You may wish to load your RSS Feed file to your internet server and
then enter the URL in one of the following validators to check the syntax.
Feed Validator - This validator validates multiple syndication formats: RSS 0.90, 0.91,
0.92, 0.93, 0.94, 1.0, 1.1, and 2.0. It includes validation for common namespaces.
RSS Validator - If you are using RSS 0.91 or RSS0.92, then you can use this validator to
validate your RSS Feed.
Experimental Online RSS 1.0, Validator - If you are using RSS 1.0, then you can use this
validator.
Redland RSS 1.0 Validator and Viewer - This is not just a validator, but also it acts as an
RSS to HTML converter.
Atom is the name of an XML-based Web content and metadata syndication format, and an
application-level protocol for publishing and editing Web resources belonging to periodically
updated websites.
Atom is a relatively recent spec and is much more robust and feature-rich than RSS. For
instance, where RSS requires descriptive fields such as title and link only in item breakdowns,
Atom requires these things for both items and the full Feed.
All Atom Feeds must be well-formed XML documents, and are identified with the
application/atom+xml media type.
Structure of an Atom 1.0 Feed
A Feed consists of some metadata, followed by any number of entries. Here is a basic structure
of an Atom 1.0 Feed.
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>...</title>
<link>...</link>
<updated>...</updated>
<author>
<name>...</name>
</author>
<id>...</id>
<entry>
<title>...</title>
<link>...</link>
<id>...</id>
<updated>...</updated>
<summary>...</summary>
</entry>
</feed>
<feed> Elements
<entry> Elements
There are some common construct, which are required for the above two elements and they are
explained in: Common Construct.
RSS originated in 1999, and has strived to be a simple, easy to understand format, with relatively
modest goals. After it became a popular format, developers wanted to extend it using modules
defined in namespaces, as specified by the W3C.
RSS 2.0 adds that capability, following a simple rule. An RSS Feed may contain elements not
described on this page, only if those elements are defined in a namespace.
The elements defined in this tutorial are not themselves members of a namespace, so that RSS
2.0 can remain compatible with the previous versions in the following sense - a version 0.91 or
0.92 file is also a valid 2.0 file. If the elements of RSS 2.0 were in a namespace, this constraint
would break, a version 0.9x file would not be a valid 2.0 file.
RSS is by no means a perfect format, but it is very popular and widely supported. Having a
settled spec is something RSS has needed for a long time.
However, the RSS spec is, for all practical purposes, frozen at version 2.0.1. But you can
anticipate the possible 2.0.2 or 2.0.3 versions, etc. only for the purpose of clarifying the
specification, not for adding new features to the format.
Subsequent work should happen in modules, using namespaces, and in completely new
syndication formats, with new names.