Unit4 - Ccs375-Webtechnologies
Unit4 - Ccs375-Webtechnologies
programming UNI T- 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Facets : Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on
XML elements are called facets.
The following example defines an element called "age" with a restriction. The value of age cannot be
lower than 0 or greater than 120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Constraint Description
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater
than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater
than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to
this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or
greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to
this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or
greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
20
Introduction to Parsers
An XML Parser is a parser that is designed to read XML and create a way for programs to use XML.
It is a software library (or a package) that provides methods (or interfaces) for client applications
to work with XML documents.
It checks the well-formattedness.
It may validate the documents.
It does a lot of other detailed things so that a client is shielded from that complexities.
Types of Parsers:
Nonvalidating parsers merely read XML documents and verify that the documents are well formed.
Validating parsers read well-formed documents in addition to checking their compliance against a DTD,
XML Schema
What is DOM?
The Document Object Model (DOM) provides a way of representing an XML document in memory so
that it can be manipulated by your software. DOM is a standard application programming interface (API)
that makes it easy for programmers to access elements and delete, add, or edit content and attributes
What DOM is not?
DOM is not a mechanism for persisting, or storing, objects as XML documents. Think of it the other way:
DOM is an object model for representing XML documents in your code. DOM is not a set of data
structures; rather it is an object model describing XML documents. DOM does not specify what
information in a document is relevant or how informationshould be structured. DOM has nothing to do
with COM, CORBA, or other technologies that include the words object model.
NEED/USE for DOM Parser
The main reason for using DOM is to create or modify an XML document programmatically.The entire
document is read into memory all at once, so you can change any part of it at any time. The representation
in memory is a tree structure that starts with root element that contains attributes, content, and sub-
elements. You can traverse this tree, search for a specific node, and change its attributes or data. You can
also add attributes or elements anywhere in the tree, as long as you don’t violate the rules of a
wellformed document. Again, you can write the modified document back out to disk or to the network.
Disadvantages of DOM
Although DOM is a W3C specification with support for a variety of programming languages,
it’s not necessarily the best solution for all problems.
One of the big issues is that DOM can be memory intensive. As mentioned earlier, when an XML
document is loaded, the entire document is read in at once.
A large document will require a large amount of memory to represent it.
Other parsing methods, such as SAX, don’t read in the entire document, so they are better in terms of
memory efficiency for some applications.
DOM Structure/DOM Core
<purchase-order>
<customer>James Bond</customer>
<merchant>Spies R Us</merchant>
<items>
<item>Night vision camera</item>
<item>Vibrating massager</item>
</items>
</purchase-order>
SAX PARSER
SAX is an API that can be used to parse XML documents. A parser is a program that reads data a
character at a time and returns manageable pieces of data. SAX provides a framework for defining event
listeners, or handlers. These handlers are written by developers interested in parsing documents with a
known structure. The handlers are registered with the SAX framework in order to receive events. Events
can include start of document, start of element, end of element, and so on. The handlers contain a number
of methods that will be called in response to these events. Once the handlers are defined and registered, an
input source can be specified and parsing can begin.
Simple SAX Example
If you parse this document using SAX, you would build a content handler by creating a Java class that
implements the ContentHandler interface in the org.xml.sax package and start the parser. the events
generated by the preceding example will look something like this:
start document
start element: fiction
start element: book (including attributes)
characters: Moby Dick
end element: book
end element: fiction
end document
SAX Packages
The SAX 2.0 API is comprised of two standard packages and one extension package.The standard
packages are org.xml.sax and org.xml.helpers. The org.xml.sax package contains the basic classes,
interfaces, and exceptions needed for parsing documents.
org.xml.sax package
<book author='William Manchester'>
The Last Lion, Winston Spencer Churchill
</book>
</biography>
<science>
<book author='Hecht, Zajac'>
Optics
</book>
</science>
</library>
***End of Document***
Example 2 : SAX Parser to Validate XML document
library.dtd
<?xml version="1.0" encoding="US-ASCII"?>
<!ELEMENT library (fiction|biography|science)*>
<!ELEMENT fictions (book)+>
<!ELEMENT biography (book)+>
<!ELEMENT science (book)+>
<!ELEMENT book (#PCDATA)>
<!ATTLIST book author CDATA #REQUIRED>
library.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<fiction>
<book author="Herman Melville">Moby Dick</book>
<book author="Zane Grey">The Last Trail</book>
</fiction>
<biography>
<book author="William Manchester">
The Last Lion, Winston Spencer Churchill
</book>
</biography>
<science>
<book author="Hecht, Zajac">Optics</book>
</science>
</library>
//SAXValidator.java
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public SAXValidator() {
valid = true;
wellFormed = true;
}
parser.setFeature("http://xml.org/sax/features/validation", true);
if (!handler.isWellFormed()) {
System.out.println("Document is NOT well formed.");
}
if (!handler.isValid()) {
System.out.println("Document is NOT valid.");
}
if (handler.isWellFormed() && handler.isValid()) {
System.out.println("Document is well formed and valid.");
}
}
}
//output
***Start of Document***
***End of Document***
Document is NOT valid.
PHP - UNIT 4
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
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the browser as plain HTML
PHP files have extension ".php"
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
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
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!" on a web page:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Creating (Declaring) PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
After the execution of the statements above, the variable $txt will hold the value Hello world!,
the variable $x will hold the value 5, and the variable $y will hold the value 10.5.
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo().
Display Text
The following example shows how to output text with the echo command (notice that the text
can contain HTML markup):
Example
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
PHP String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Arrays
An array is a linear data structure in which we store homogeneous data elements in contigous
memory locations.
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Get The Length of an Array - The count() Function
The count() function is used to return the length (the number of elements) of an array:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
HP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Sort Array in Ascending Order - sort()
Example
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>
programs in php
program 1 - Sieve of Eratosenses
<?php
// put your code here
echo $_POST['t1'];
$len = 1000;
$array1[0]=1;
for($i =0;$i<$len;$i++)
{
$array1[$i]=1;
}
for($i=2;$i<$len;$i++)
{
$inc=$i;
for($j=$i+$inc; $j<$len; $j += $inc)
{
$array1[$j]=0;
}
?>
<h1> List of Prime numbers below 1000 </h1>
<h2>Sieve of Eratosthemes</h2>
<ol>
<?php
for($i =0;$i<$len;$i++)
{
if($array1[$i]==1)
echo "<li>".$i."</li>";
?>
</ol>
PHP Form Validation Example
filename:welcome.php
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
PHP 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.
Create Cookies With PHP
A cookie is created with the setcookie() function.
Syntax
setcookie(name, value, expire);
Only the name parameter is required. All other parameters are optional.
<?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];
}
?>
<p><strong>Note:</strong> You might have to reload the page to see the value of the
cookie.</p>
</body>
</html>
Output
Cookie 'user' is set!
Value is: John Doe
Note: You might have to reload the page to see the value of the cookie.
PHP Date
Example
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : int {
return (int)($a + $b);
}
echo addNumbers(1.2, 5.2);
?>
output: 6
The first parameter of fopen() contains the name of the file to be opened and the second
parameter specifies in which mode the file should be opened. The following example also
generates a message if the fopen() function is unable to open the specified file:
Example1
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
using fgets()
<!DOCTYPE html>
<html>
<body>
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
</body>
</html>