Integrative Programming and Technologies (Itec4121)
Integrative Programming and Technologies (Itec4121)
Integrative Programming and Technologies (Itec4121)
(ITec4121)
Chapter Three
XML and XML Related Technologies
Introduction
2
Features of XML
3
XML Document
Example 1:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>John</to>
<from>Ahmed</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The first line is the XML declaration. It defines the XML version (1.0)
and the encoding used (ISO-8859-1 = Latin-1/West European character
set).
The next line describes the root element of the document (like saying:
"this document is a note"):
<note>
4
XML Document…
The next 4 lines describe 4 child elements of the root (to, from,
heading, and body).
<to>John</to>
<from>Ahmed</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
And finally the last line defines the end of the root element.
</note>
Note: XML documents must contain a root element. This element is "the
parent" of all other elements
The elements in an XML document form a document tree.
5
XML Document…
6
XML Document…
Example:
<?xml version="1.0"?>
<University>
<student>
<firstname>Abdi</firstname>
<lastname>Kemal</lastname>
<contact>0999044993</contact>
<email>abdikemal@gmail.com</email>
<address>
<city>Ambo</city>
<state>Oromia</state>
<pin>201007</pin>
</address>
</student>
</University>
7
XML Related Technologies
8
XML Related Technologies…
9
XML Related Technologies…
10
XML Attributes
XML elements can have attributes which are used to add the information about the
element.
XML attributes enhance the properties of the elements.
XML attributes must always be quoted. We can use single or double quote.
Example:
<book publisher="Tata McGraw Hill"></book>
Or
<book publisher='Tata McGraw Hill'></book>
Metadata should be stored as attribute and data should be stored as element.
<book>
<book category="computer">
<author> A & B </author>
</book>
11
XML Attributes…
All elements can have text content and attributes (just like in HTML).
13
XML Comments
14
XML Comments…
Example:
<?xml version="1.0" encoding="UTF-8" ?>
<!--Students marks are uploaded by months-->
<students>
<student>
<name>Daba</name>
<marks>70</marks>
</student>
<student>
<name>Almaz</name>
<marks>60</marks>
</student>
</students>
15
XML Comments…
16
XML Validation
17
XML Validation…
18
XML Validation…
19
Checking Validation using DTD
Before proceeding with XML DTD, you must check the validation. An
XML document is called "well-formed" if it contains the correct
syntax.
A well-formed and valid XML document is one which has been
validated against DTD.
Example: well-formed and valid XML document.
employee.xml
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>Abebe</firstname>
<lastname>Zewdie</lastname>
<email>abewinta@gmail.com</email>
</employee>
20
Checking Validation using DTD…
21
Checking Validation using DTD…
Description of DTD:
<!DOCTYPE employee : defines that the root element of the
document is employee.
<!ELEMENT employee: defines that the employee element contains 3
elements "firstname, lastname and email".
<!ELEMENT firstname: defines that the firstname element is
#PCDATA typed. (parse-able data type).
<!ELEMENT lastname: defines that the lastname element is
#PCDATA typed. (parse-able data type).
<!ELEMENT email: defines that the email element is #PCDATA
typed. (parse-able data type).
22
Checking Validation using DTD…
23
Checking Validation using DTD…
Example:
author.xml
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ENTITY jm "John Michael">
]>
<author>& jm;</author>
In the above example, jm is an entity that is used inside the author
element. In such case, it will print the value of jm entity that is "John
Michael".
Note: A single DTD can be used in many XML files
24
XML CSS with DTD
25
XML CSS with DTD…
27
XML CSS with DTD…
28
XML Schema
29
Checking Validation with XSD
A well-formed and valid XML document is one which has been validated
against Schema.
Example: Create a schema file:
30
Checking Validation with XSD…
31
Checking Validation with XSD…
32
DTD vs. XSD
33
CDATA and PCDATA
34
CDATA and PCDATA…
In the above CDATA example, CDATA is used just after the element
employee to make the data/text unparsed, so it will give the value of
employee:
<firstname>Abebe</firstname><lastname>Zewdie</lastname><email>abewinta@gmail.com</email>
36
XML Parsers
37
Types of XML Parsers
38
SAX (Simple API for XML)
A SAX Parser implements SAX API. This API is an event based API and less
intuitive.
Features of SAX Parser:
It does not create any internal structure.
Clients does not know what methods to call, they just overrides the methods
of the API and place his own code inside method.
It is an event based parser; it works like an event handler in Java.
It is simple and memory efficient.
It is very fast and works for huge documents.
It is event-based so its API is less intuitive.
Clients never know the full information because the data is broken into pieces
39
XML DOM
40
XML DOM…
41
XML DOM…
We can modify or delete their content and also create new elements.
The elements, their content (text and attributes) are all known as nodes.
For example, consider this table, taken from an HTML document:
<TABLE>
<ROWS>
<TR>
<TD>A</TD>
<TD>B</TD>
</TR>
<TR>
<TD>C</TD>
<TD>D</TD>
</TR>
</ROWS>
</TABLE>
42
XML DOM…
43
Example 1: Load XML File
note.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>sonoojaiswal@javatpoint.com</to>
<from>vimal@javatpoint.com</from>
<body>Hello XML DOM</body>
</note>
44
Example 1: Load XML File…
The HTML file that extracts the data of XML document using DOM:
xmldom.html
<!DOCTYPE html>
<html>
<body>
<h1>Important Note</h1>
<div>
<b>To:</b> <span id="to"></span><br>
<b>From:</b> <span id="from"></span><br>
<b>Message:</b> <span id="message"></span>
</div>
45
Example 1: Load XML File…
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
46
Example 1: Load XML File…
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")
[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")
[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")
[0].childNodes[0].nodeValue;
</script>
</body>
</html>
47
XML Database
48
Types of XML databases
1. XML-enabled database
2. Native XML database (NXD)
XML-enable Database:
It works just like a relational database.
It is like an extension provided for the conversion of XML documents.
It stores data in a table in the form of rows and columns.
Native XML Database:
It stores large amount of data.
Instead of table format, it is based on container format.
You can query data by XPath expressions
It is preferred over XML-enable database because it is highly capable
to store, maintain and query XML documents.
49
Example of XML database
<?xml version="1.0"?>
<contact-info>
<contact1>
<name>Abebe Zewdie</name>
<company>Ambo University</company>
<phone>(0120) 4256464</phone>
</contact1>
<contact2>
<name>John Michael </name>
<company>Ambo University</company>
<phone>09990449935</phone>
</contact2>
</contact-info>
In the above example, a table named contacts is created and holds the contacts
(contact1 and contact2). Each one contains 3 entities name, company and phone.
50
XML Namespaces
53
XML Namespaces…
56
XML Namespaces…
57
The Default Namespace
58