Element of Commercial Portal Unit II
Element of Commercial Portal Unit II
XML is a software- and hardware-independent tool for storing and transporting data.
What is XML?
Why xml
Platform Independent and Language Independent: The main benefit of xml is that you can use it
to take data from a program like Microsoft SQL, convert it into XML then share that XML with
other programs and platforms. You can communicate between two platforms which are generally
very difficult.
The main thing which makes XML truly powerful is its international acceptance. Many
corporation use XML interfaces for databases, programming, office application mobile phones
and more. It is due to its platform independent feature.
But still, the XML above does not DO anything. XML is just information wrapped in tags.
Someone must write a piece of software to send, receive, store, or display it:
Note
To: Tove
From: Jani
Reminder
Don't forget me this weekend!
The tags in the example above (like <to> and <from>) are not defined in any XML standard.
These tags are "invented" by the author of the XML document.
HTML works with predefined tags like <p>, <h1>, <table>, etc.
With XML, the author must define both the tags and the document structure.
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 also makes it easier to expand or upgrade to new operating systems, new applications, or
new browsers, without losing data.
With XML, data can be available to all kinds of "reading machines" like people, computers,
voice machines, news feeds, etc.
XML Example 1
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Output
Note
To: Tove
From: Jani
Heading: Reminder
XML Example 2
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
With XML, data can be stored in separate XML files. This way you can focus on using
HTML/CSS for display and layout, and be sure that changes in the underlying data will not
require any changes to the HTML.
With a few lines of JavaScript code, you can read an external XML file and update the data
content of your web page.
XML data is stored in plain text format. This provides a software- and hardware-independent
way of storing data.
This makes it much easier to create data that can be shared by different applications.
Exchanging data as XML greatly reduces this complexity, since the data can be read by different
incompatible applications.
XML data is stored in text format. This makes it easier to expand or upgrade to new operating
systems, new applications, or new browsers, without losing data.
With XML, your data can be available to all kinds of "reading machines" (Handheld computers,
voice machines, news feeds, etc), and make it more available for blind people, or people with
other disabilities.
o XHTML
o WSDL for describing available web services
o WAP and WML as markup languages for handheld devices
o RSS languages for news feeds
o RDF and OWL for describing resources and ontology
o SMIL for describing multimedia for the web
XML Example
XML documents create a hierarchical structure looks like a tree so it is known as XML Tree that
starts at "the root" and branches to "the leaves".
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>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body).
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal 6
<body>Don't forget me this weekend!</body>
And finally the last line defines the end of the root element.
</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. The tree starts at the root and
branches to the lowest level of the tree.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the relationships between elements.
Parent elements have children. Children on the same level are called siblings (brothers or sisters).
XML Element
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.
<price>29.99</price>
text
attributes
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<title>, <author>, <year>, and <price> have text content because they contain text (like 29.99).
<bookstore> and <book> have element contents, because they contain elements.
<element></element>
<element />
The two forms produce identical results in XML software (Readers, Parsers, Browsers).
Create short and simple names, like this: <book_title> not like this: <the_title_of_the_book>.
Avoid "-". If you name something "first-name", some software may think you want to subtract
"name" from "first".
Avoid ".". If you name something "first.name", some software may think that "name" is a
property of the object "first".
Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your
software doesn't support them!
Naming Conventions
Some commonly used naming conventions for XML elements:
Camel <firstName> Uppercase first letter in each word except the first
case (commonly used in JavaScript)
XML documents often have a corresponding database. A common practice is to use the naming
rules of the database for the XML elements.
XML Attributes
XML elements can have attributes, just like HTML.
For a person's gender, the <person> element can be written like this:
<person gender="female">
or like this:
<person gender='female'>
If the attribute value itself contains double quotes you can use single quotes, like in this example:
<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
There are no rules about when to use attributes or when to use elements in XML.
<messages>
<note id="501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note id="502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
</messages>
The id attributes above are for identifying the different notes. It is not a part of the note itself.
What I'm trying to say here is that metadata (data about data) should be stored as attributes, and
the data itself should be stored as elements.
XML stands for Extensible Markup Language. It is also a markup language designed especially
for web documents. It defines a set of rules for encoding documents in a format that is both
human-readable and machine-readable. It allows developers to create custom tags. XML also
enables the definition, transmission, validation, and interpretation of data between applications.
We can use CSS properties to style the content present in the XML document. Following are the
steps to display XML using CSS −
Example
In the following example, we are creating an XML file that contains information about cricket
player statistics and displaying the XML file using CSS.
<?xml version="1.0"?>
<style>
Cricket {
display: block;
margin-bottom: 30px;
name {
font-weight: bold;
color: seagreen;
hundreds, fifties {
color: lightslategray;
</style>
<Cricket>
<name>Virat Kohli</name>
<hundreds>75</hundreds>
<fifties>130</fifties>
</Cricket>
<Cricket>
<name>Joe Root</name>
<hundreds>46</hundreds>
<fifties>99</fifties>
</Cricket>
<Cricket>
<name>Steve Smith</name>
<hundreds>44</hundreds>
<fifties>70</fifties>
</Cricket>
<Cricket>
<name>Faf du Plessis</name>
<hundreds>23</hundreds>
<fifties>66</fifties>
</Cricket>
</Sports>
Output
Virat Kohli 75 130
Joe Root 46 99
Steve Smith 44 70
Faf du Plessis 23 66
Example
data.xml
<root>
<person>
<name>Maya</name>
<age>30</age>
<gender>Female</gender>
</person>
<person>
<name>Ram</name>
<age>25</age>
<gender>Male</gender>
<person>
<name>Varun</name>
<age>25</age>
<gender>Male</gender>
</person>
<person>
<name>Sarah</name>
<age>25</age>
<gender>Female</gender>
</person>
</root>
CSS File
root {
display: block;
font-size: 14px;
margin-bottom: 20px;
person {
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
16
display: block;
width: 10%;
display: block;
margin-bottom: 20px;
padding: 10px;
border-radius: 5px;
name {
font-weight: bold;
color: #333;
age {
color: #999;
gender{
color: brown;
font-weight: bolder;
XML Data Source Object is a Microsoft Active X object built into the web browser so we can
use ActiveX control to extract data from the XML code embedded directly in the HTML file and
the external XML file using data binding into HTML web pages.
Data islands
2. Implicit method: Embedding XML data with reference to external XML file.
Syntax:
<XML ID=“xmlID” SRC=“filename.xml”></XML>
The DSO object is implicitly created when we use a XML data island.
To extract data from external data file use the HTML tags.
<IMG>, <LABLE>, <TABLE>
<html>
<head>
</head>
<body>
<xml id="xml">
<table datasrc="#xml">
<thead>
<th>Name</th>
<th>gender</th>
</thead>
<tr>
<td><div datafld="name"></div></td>
<td><div datafld="gender"></div></td>
</tr>
</table>
<db>
<member>
<name>BOB</name>
<gender>male</gender>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
19
</member>
</db>
</xml>
</body>
</html>
Output:
Name gender
Bob Male
Syntax:
<OBJECT ID=“SomeID CLASSID=CLSID”></OBJECT>
Load the external XML file in HTML page using the XMLDSO. We have to use Javascript in
the <HEAD> section of HTML page.
Extract data from loaded XML file through HTML tags using DATASRC and DATAFLD.
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying
to mix XML documents from different XML applications.
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
20
This XML carries information about a table (a piece of furniture):
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a
<table> element, but the elements have different content and meaning.
A user or an XML application will not know how to handle these differences.
This XML carries information about an HTML table, and a piece of furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements have different
names.
The namespace can be defined by an xmlns attribute in the start tag of an element.
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="https://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
The xmlns attribute in the first <table> element gives the h: prefix a qualified namespace.
The xmlns attribute in the second <table> element gives the f: prefix a qualified namespace.
When a namespace is defined for an element, all child elements with the same prefix are
associated with the same namespace.
<root xmlns:h=http://www.w3.org/TR/html4/
xmlns:f="https://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
Note: The namespace URI is not used by the parser to look up information.
However, companies often use the namespace as a pointer to a web page containing namespace
information.
XML DTD
An XML document with correct syntax is called "Well Formed".
An XML document validated against a DTD is both "Well Formed" and "Valid".
What is a DTD?
DTD stands for Document Type Definition.
A DTD defines the structure and the legal elements and attributes of an XML document.
With a DTD, you can verify that the data you receive from the outside world is valid.
If you develop applications, wait until the specification is stable before you add a DTD.
Otherwise, your software might stop working because of validation errors.
The DOCTYPE declaration above contains a reference to a DTD file. The content of the DTD
file is shown and explained below.
!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"
Elements
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
25
Attributes
Entities
PCDATA
CDATA
Elements
Elements are the main building blocks of both XML and HTML documents.
Examples of HTML elements are "body" and "table". Examples of XML elements could be
"note" and "message". Elements can contain text, other elements, or be empty. Examples of
empty HTML elements are "hr", "br" and "img".
Examples:
<body>some text</body>
<message>some text</message>
Attributes
Attributes provide extra information about elements.
Attributes are always placed inside the opening tag of an element. Attributes always come in
name/value pairs. The following "img" element has additional information about a source file:
The name of the element is "img". The name of the attribute is "src". The value of the attribute is
"computer.gif". Since the element itself is empty it is closed by a " /".
Entities
Some characters have a special meaning in XML, like the less than sign (<) that defines the start
of an XML tag.
Most of you know the HTML entity: " ". This "no-breaking-space" entity is used in
HTML to insert an extra space in a document. Entities are expanded when a document is parsed
by an XML parser.
< <
& &
" "
' '
PCDATA
PCDATA means parsed character data.
Think of character data as the text found between the start tag and the end tag of an XML
element.
PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser
for entities and markup.
Tags inside the text will be treated as markup and entities will be expanded.
However, parsed character data should not contain any &, <, or > characters; these need to be
represented by the & < and > entities, respectively.
CDATA
CDATA means character data.
CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated
as markup and entities will not be expanded.
XML Schema
An XML Schema describes the structure of an XML document.
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>
The purpose of an XML Schema is to define the legal building blocks of an XML document:
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.
XSLT Example
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
SAX Parser
Using the SAX, DOM, etc., that are used as parsers, we can parse the XML file in Android. The
SAX parser can be used to parse the XML file only and not to create the XML file.
Example:
activity_main.xml:
List_row.xml:(File: list_row.xml)
XML parser validates the document and check that the document is well formatted.
Let's understand the working of XML parser by the figure given below:
1. DOM
2. SAX
Advantages
1) It supports both read and write operations and the API is very simple to use.
Disadvantages
1) It is memory inefficient. (consumes more memory because the whole XML document needs
to loaded into memory).
Clients does not know what methods to call, they just overrides the methods of the API and place
his own code inside method.
Advantages
1) It is simple and memory efficient.
Disadvantages
1) It is event-based so its API is less intuitive.
2) Clients never know the full information because the data is broken into pieces.
As a W3C specification, one important objective for the Document Object Model is to provide a
standard programming interface that can be used in a wide variety of environments and
applications. The Document Object Model can be used with any programming language.
XML DOM defines a standard way to access and manipulate XML documents.
We can modify or delete their content and also create new elements. The elements, their content
(text and attributes) are all known as nodes.
<TABLE>
<ROWS>
<TR>
<TD>A</TD>
<TD>B</TD>
</TR>
<TR>
<TD>C</TD>
<TD>D</TD>
</TR>
</ROWS>
</TABLE>
This example parses an XML document (note.xml) into an XML DOM object and extracts information from it with
JavaScript.
note.xml
Let's see 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>
<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;
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>
Why SOAP?
It is important for web applications to be able to communicate over the Internet.
The best way to communicate between applications is over HTTP, because HTTP is supported
by all Internet browsers and servers. SOAP was created to accomplish this.
Syntax Rules
Here are some important syntax rules:
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
Example
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
Message information goes here
...
</soap:Envelope>
If a different namespace is used, the application generates an error and discards the message.
Syntax
soap:encodingStyle="URI"
Example
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
Message information goes here
...
</soap:Envelope>
If the Header element is present, it must be the first child element of the Envelope element.
Note: All immediate child elements of the Header element must be namespace-qualified.
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
The example above contains a header with a "Trans" element, a "mustUnderstand" attribute with
a value of 1, and a value of 234.
The attributes defined in the SOAP Header defines how a recipient should process the SOAP
message.
If you add mustUnderstand="1" to a child element of the Header element it indicates that the
receiver processing the Header must recognize the element. If the receiver does not recognize the
element it will fail when processing the Header.
Syntax
soap:mustUnderstand="0|1"
Example
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
The SOAP actor attribute is used to address the Header element to a specific endpoint.
Example
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:actor="https://www.w3schools.com/code/">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
Syntax
soap:encodingStyle="URI"
Example
<?xml version="1.0"?>
<soap:Envelope
<soap:Body>
<m:GetPrice xmlns:m="https://www.w3schools.com/prices">
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
The example above requests the price of apples. Note that the m:GetPrice and the Item elements
above are application-specific elements. They are not a part of the SOAP namespace.
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPriceResponse xmlns:m="https://www.w3schools.com/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>
The SOAP Fault element holds errors and status information for a SOAP message.
If a Fault element is present, it must appear as a child element of the Body element. A Fault
element can only appear once in a SOAP message.
The faultcode values defined below must be used in the faultcode element when describing
faults:
Error Description
Server There was a problem with the server so the message could not
proceed