Menus

Sep 27, 2015

Introduction to XML

XML stands for EXtensible Markup Language.
XML is a markup language much like HTML.
XML is a subset of SGML (Standard Generalized Markup Language).
XML tags are not predefined. You must define your own tags.
XML was designed to transport and store data, with focus on what data is, whereas HTML was designed to display data, with focus on how data looks.

XML is used in many aspects of web development, often to simplify data storage and sharing.
Once the data is on the client machine, it can be manipulated, edited and presented in multiple views without return trips to the server.


XML Separates Data from the Presentation and the Process. The power and beauty of XML is that it maintains the separation of the user interface from the structured data.

HTML is about displaying information, while XML is about carrying information.

XML is defined by 4 specification
XML :
Defines the syntax of XML.

XLL (Extensible, Linking Language):
Defines a standard way to represent links between resources.

XSL (Extensible Style Language):
Will define a standard style sheet language for XML.
XUA (XML User Agent):
Will help standardize XML User Agents (like browser).

Writing Well-Formed XML Documents
An XML document is well-formed if it follows some basic rules. The XML format has a simpler set of parsing rules than HTML, allowing an XML parser to read and expose XML data without an external description or special knowledge about the meaning of the XML data.

Basic rules for Well-Formed XML documents:

Start tags and End tags must match:
Each start tag must have a corresponding end tag.

Elements cannot overlap:
Wrong overlapping:
<title>Sample XML<Sub>Introduction to XML</title></sub>


Correct overlapping:
<Title>
<sub>Introduction</sub>
<author>Mr Carry</author>
</Title>

XML tags are case-sensitive
The following tags represent different element:
<City> <CITY> <city>

Empty elements:
A sole tag ending with ‘/>’ signal that the element has no contents.  For example, the following two lines are equivalent:
  <title />
<title></title>

Reserved characters
Several characters are part of the syntactic structure of XML and will not be interpreted as themselves if simply placed within an XML data source.
Desired character
Character Sequence
< 
&lt;
&
&amp;
> 
&gt;
&quot;
&apos;

Each XML document must have a unique root element: ( XML Tree)
XML documents form a tree structure that starts at "the root" and branches to "the leaves". Several characters are part of the syntactic structure of XML and will not be interpreted as themselves if simply placed within an XML data source.
<LGIC>
<Courses>
<BBA>
<RegdNo>001</RegdNo>
<Name>Himesh Pokharel</Name>
<Address>Pokhara 10</Address>
<Phone>9846033212</Phone>
</BBA>
<BBA>
<RegdNo>002</RegdNo>
<Name>Rakesh Mishra</Name>
<Address>Pokhara 4</Address>
<Phone>9846067212</Phone>
</BBA>
<BBA>
<RegdNo>003</RegdNo>
<Name>Iron Dhoni</Name>
<Address>KTM 10</Address>
<Phone>9841334212</Phone>
</BBA>
</Courses>
</LGIC>

How do Browsers read XML?
A tool for reading XML documents is popularly called a ‘XML parser’, though the more formal name is a ‘XML processor’.
XML processors parse data to an application for authoring, publishing, searching or displaying.
XML doesn’t provide an API to an application, it just passes data to it.
Most of new Browser already included XML parser.

How do XML document looks?
<?xml version=“1.0” ?>
<Mail>
<to>John</to>
<from>Mary</from>
<heading>Reminder</heading>
<body>Don’t forget me this weekend!</body>
</Mail>

The first line in document – ‘the XML declaration’ – should always be included. It defines the XML version of the document.
The next line describe the Root Element of the document.
The next 4 lines describe 4 Child Elements of the root.
And finally the last line defines the end of the root element.

What is Prolog?
The part of a XML document that precedes the first start tag is collectively known as the prolog.
XML documents should start with a prolog that describes the XML version, document type and other characteristics of the document.
Syntax:
<?xml version=“1.0”?>
<!DOCTYPE DOCBOOK SYSTEM “http://www.eduus.com/docbook”>
This DTD says that the document conforms to XML versin1.0 and declares adherence to particular document type DOCBOOK.
Document Type Definition (DTD)
A specification for a SGML or HTML document that specifies structural elements and markup definitions that can be used to create documents that describe content.
The DTD can put constraints on the occurrence and content of elements and other details of the document structure.

Document Type Declaration
A document type declaration is a statement embedded in a XML document whose purpose is to acknowledge the existence and location of a document type defination(DTD).

A document type declaration is different from document type definition.
The first is a statement that points to the second.

A DTD is a set of rules that defines the structure of an XML document whereas, a document type declaration is a statements that tells the parser which DTD to use for checking and validation.

All document type declaration starts with string “<!DOCTYPE”>.
The DTD declaration can be internal or external.
If external then the DTD must be specified either as “SYSTEM” or “PUBLIC” in the document type declaration.
If “PUBLIC” the DTD can be used by anyone by refering the URL.
If “SYSTEM” that means it may not be available for use by other applications.

CDATA Sections
In a document, a CDATA section instructs the parser not to interpret the data defined within it as a markup. CDATA stands for “Character Data”.
Syntax:  <! { CDATA [content]}>

Between the start of the section, “<!CDATA[“ and the end of the section “]}>” all character data is passed directly to the application.
The only string that cannot occur in a CDATA section is “]}>”. Which known as CDEnd.

XML Elements
Elements are the most common form of markup.
Delimited by angle brackets, most elements identify the nature of the content they surround.
XML elements breakdown into two category.
The elements which have content, elements or both are called content element.
An element which doesn’t have content or elements are called Empty element.

XML Attributes
Attributes are a way of attaching characteristics or properties to elements of a document.
Attributes are name-value pairs that occur inside tags after the element name.
In XML, all attribute values must be quoted by double quotes or single quotes.
Attributes cannot contain any sub-attributes.
Example: <person name=“Mr.John”>

Internal DTD example
<?xml version="1.0"?>
<!DOCTYPE Mail[
<!ELEMENT Mail (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<Mail>
<to>Peter</to>
<from>Julia </from>
<heading>Reminder</heading>
<body>Don't forget me!</body>
</Mail>
!ELEMENT Mail  defines the element “Mail” having four elements: “to, from, heading & body”.
!ELEMENT to defines the element to be type “CDATA”.
And !ELEMENT from, heading & body elements be of type “CDATA”.

External DTD example
The XML file
<?xml version="1.0"?>
<!DOCTYPE Mail SYSTEM “mail.dtd”>
<Mail>
<to>Peter</to>
<from>Mary</from>
<heading>Reminder</heading>
<body>Don't forget me!</body>
</Mail>
The file “mail.dtd” containing the DTD:
<?xml version=“1.0”?>
<!ELEMENT Mail (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Element Declaration
Element type declarations must start with the string: “<!ELEMENT”, followed by the name and finally content specification.
Every element has certain allowed content.
There are four kinds of content specification.
Content Specification Type
Description
EMPTY content
May not have content.
ANY content
May have any content at all
Mixed content
May have character data or a mix of character data and sub elements
Element content
May have only sub-elements
Example of Content Specification
Empty content
<!ELEMENT myfirstelement EMPTY>
ANY content
<!ELEMENT mysecondelement ANY>
Mixed content
<!ELEMENT language (#PCDATA)>
Element content
<!ELEMENT mail (to, from, subject, body)>

Occurrence Indicators
XML also allows you to specify whether a content particle is optional or repeatable using an occurrence indicator. There are three occurrence indicators:

Occurrence Indicator
Description
?
Optional (0 or 1 time)
*
Optional and repeatable (0 or more times)
+
Required and repeatable (1 or more times)

Associating CSS Style Sheet with XML
XML has emerged as a "universal" data format in a variety of application areas.
Style sheets are an essential step in XML deployment to define the presentation of XML documents.
The association consists of inserting the XML processing instruction at the top of the document, before the root element of the XML document and after the XML prolog.
The processing instruction has two required attributes type and href which respectively specify the type of stylesheet (Internet Media Type text/css) and its address (path).
<?xml-stylesheet type="text/css" href="foo.css"?>

Example of CSS with XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<PU>
<Centers>
<Center>
<Code>001</Code>
<Location id="Pkr">Pokhara</Location>
<Phone>344333</Phone>
<Email>pkr@webcom.com</Email>
</Center>
<Center>
<Code>003</Code>
<Location id="Ktm">Kathmandu</Location>
<Phone>3444433</Phone>
<Email>ktm@webcom.com</Email>
</Center>
<Center>
<Code>004</Code>
<Location id="Tnu">Tanahu</Location>
<Phone>342223</Phone>
<Email>tanahu@webcom.com</Email>
</Center>
</Centers>
</PU>

Secondly create “style.css” file
PU {
font-family: verdana;
color:brown;
}
Center {
background:yellow;
display:block;
margin:5px;
}
Location {
font-size:large;
display:block;
}
Email {
font-size:small;
display:block;
}

XML Schema
XML Schema is an XML-based language used to create XML-based languages and data models.
An XML schema defines element and attribute names for a class of XML documents.
The schema also specifies the structure that those documents must adhere to and the type of content that each element can hold.
DTDs are similar to XML schemas in that they are used to create classes of XML documents.

As a means of understanding the power of XML Schema, let's look at the limitations of DTD.
DTDs do not have built-in datatypes.
DTDs do not support user-derived datatypes.
DTDs allow only limited control over cardinality (the number of occurrences of an element within its parent).
DTDs do not support Namespaces or any simple way of reusing or importing other schemas.
An XML schema describes the structure of an XML instance document by defining what each element must or may contain.
An element is limited by its type. For example, an element of complex type can contain child elements and attributes, whereas a simple-type element can only contain text.
Example of XML Schema

The “schema.xsd” file:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="p" type="xsd:string"/>
</xsd:schema>

The “schema.xml” file:
<?xml version="1.0"?>
<p>Hello world!</p>

XML – JavaScript

The XMLHttpRequest Object
The XMLHttpRequest object is used to exchange data with a server behind the scenes.
Using XMLHttpRequest object you can:
Update a web page without reloading the page
Request data from a server after the page has loaded
Receive data from a server after the page has loaded
Send data to a server in the background

Create an XMLHttpRequest Object
All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) have a built-in XMLHttpRequest object.
Syntax for creating an XMLHttpRequest object:
xmlhttp=new XMLHttpRequest();
Old versions of Internet Explorer (IE5 and IE6) use an ActiveX Objects
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

XML Parser
All modern browsers have a built-in XML parser.
An XML parser converts an XML document into an XML DOM object - which can then be manipulated with JavaScript.
The following code fragment parses an XML document into an XML DOM object:
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
xmlhttp.open("GET","books.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

XML DOM
The XML DOM defines a standard way for accessing and manipulating XML documents.
The XML DOM views an XML document as a tree-structure.
All elements can be accessed through the DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can be created. The elements, their text, and their attributes are all known as nodes.

Example
Create a XML file with filename: “myxml.xml”
<?xml version="1.0" ?>
<LGCI>
<BBA>
<ID>11</ID>
<Name>Ram Gurung</Name>
<Address>Pokhara</Address>
</BBA>
<BBA>
<ID>22</ID>
<Name>Bibek Shrestha</Name>
<Address>Lekhnath</Address>
</BBA>
</LGCI>

Create a HTML file with filename: “XMLtoHTML.xml”
<html>
<body>
<script>
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET", "myxml.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName("BBA");
for(i=0;i<x.length;i++)
{
document.write(x[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue);
document.write(x[i].getElementsByTagName("AddrFess")[0].childNodes[0].nodeValue);
}
</script>
</body>
</html>


No comments:

Post a Comment

Contact Form

Name

Email *

Message *