WP Unit5
WP Unit5
XML
HTML XML
1.HTML is used to display data and 1.XML is a software and hardware independent
focuses on how data looks. tool used to transport and store data.
2. HTML is a markup language itself. 2.XML provides a framework to define markup
languages.
3. HTML is not case sensitive. 3. XML is case sensitive.
4.HTML tags are predefined. 4. We can define tags according to our need.
5.In HTML, it is not necessary to use a 5.XML makes it mandatory to use a closing tag.
closing tag.
6.HTML is static because it is used to 6.XML is dynamic because it is used to
display data. transport data.
7.HTML does not preserve whitespaces. 7.XML preserve whitespaces.
<html> <student>
<body> <name>Divya</name>
<h1>Divya</h1> <section>III AIDS</section>
<h2>III AIDS</h2> <total>234</total>
<h3>234</h3> </student>
</body>
</html>
XML is widely used in the era of web development. It is also used to simplify data storage and
data sharing. The main features or advantages of XML are given below.
1
1) XML separates data from HTML
If we need to display dynamic data in your HTML document, it will take a lot of work to edit the
HTML each time the data changes. With XML, data can be stored in separate XML files.
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.
In the real world, computer systems and databases contain data in incompatible formats. 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.
One of the most time-consuming challenges for developers is to exchange data between
incompatible systems over the Internet. Exchanging data as XML greatly reduces this
complexity, since the data can be read by different incompatible applications.
Upgrading to new systems (hardware or software platforms), is always time consuming. Large
amounts of data must be converted and incompatible data is often lost. 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.
Different applications can access your data, not only in HTML pages, but also from XML data
sources. 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.
A lot of new Internet languages are created with XML. Here are some examples:
XHTML, WSDL for describing available web services, WAP and WML as markup languages
for handheld devices, RSS languages for news feeds, RDF and OWL for describing resources
and ontology, SMIL for describing multimedia for the web.
Let us discuss the syntax of the XML document with the help of the following example.
XML Declaration:
The XML declaration indicates that the document is written in XML and specifies which version
of XML. <?xml version=”1.0”?> This instruction tells the parser that the data in the file
follows the rules of XML version 1.0. This instruction must be the first instruction in your XML
file.
Comments:
XML comments are the same as HTML comments begin with <!-- and end with -->. The same
type of comments are used in both XML source files and in DTD files.
Processing Instructions:
Processing instructions begin with <? and end with ?>. Processing instructions are instructions
for the XML processor to control applications.
XML elements:
XML elements are represented by tags. Elements usually consist of an opening tag and a closing
tag, but they can consist of just one tag. Opening tags consist of <, followed by the element
name, and ending with >. Closing tags are the same but have a forward slash inserted between
the less than symbol and the element name. XML elements are case sensitive.<book> is different
from <BOOK>. XML elements must be nested properly.XML elements may have attributes.
Syntax: <tag>Data</tag>
Example: <author>Bala guruswamy</author>
3
4. Explain about Document Type Definition: (DTD) ?
DTD stands for Document Type Definition. A DTD allows you to create rules for the elements
within your XML documents. Although XML itself has rules, the rules defined in a DTD are
specific to your own needs.
The DTD is declared at the top of your XML document. The actual contents of the DTD can be
included within your XML document or included in another document and linked to (or both).
The XML document contains data where as DTD file contains rules that apply to the data. A
DTD is a file with a .dtd (filename.dtd) extension.
Syntax of DTD:
<!DOCTYPE root-element
[
<!ELEMENT ELEMENTNAME-1 (sub elements list)>
<!ELEMENT ELEMENTNAME-2 (data-type)>
-
-
-
<!ELEMENT ELEMENTNAME-N (data-type)>
<!ATTLIST element-name attribute-name attribute-type default-value>
]>
DTD elements:
In the DTD, XML elements are declared with an element declaration. An element
declaration has the following syntax:
Here, element_name is the name of the element you're defining, followed by a data-type or
another element needs to be surrounded by brackets . Data types are
CDATA: CDATA is plain text character data which is not parsed through the engine of the
XML parser.
PCDATA: PCDATA is parsed character data which may contain XML markup and hence has to
be handle by the parser.
The default data type for elements is PCDATA , but CDATA can be very useful. If the content
of the element contains any of the characters which are used for markup such as <, > or & you
will not want the parser to handle these. If they are parsed then you may get errors about the
structure of your document. The use of CDATA lets you avoid parsing.
Element names:
You can use any name you like for your elements as long as they adhere to the following rules:
4
Element names can contain any character (including letters and numbers)
Element names must not contain spaces
Element names must not begin with a number or punctuation character (for example a
comma or semi-colon etc)
Element names must not start with the letters xml (whether lowercase, uppercase, or
mixed case)
You shouldn't use a colon (:) in your element names, as this is reserved for another purpose.
Empty elements:
Empty elements are declared with the keyword EMPTY inside the parentheses:
Syntax: <! ELEMENT element-name (EMPTY) >
Example: <! ELEMENT header (EMPTY)>
DTD attributes:
In DTD, XML elements attributes are declared with an <!ATTLIST> declaration. An attribute
declaration has the following syntax:
The ATTLIST declaration defines the element which can have the attribute, the name of the
attribute, the type of the attribute, and the default attribute value.
DTD example:
<! ATTLIST payment type CDATA “check” >
XML example:
<payment type=”check”>
Types of DTD:
The only difference between internal and external is in the way it's declared with <!
DOCTYPE>.
In this we enclose the DTD in a <DOCTYPE> element providing the name of the root
element of the document as (<! DOCTYPE note). If the DTD is declared inside the XML file,
it should be wrapped in a DOCTYPE definition with the following syntax:
5
Example:
<?xml version="1.0"?>
<!DOCTYPE tutorials
[
<!ELEMENT tutorials (tutorial)+>
<!ELEMENT tutorial (name,url)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ATTLIST tutorials type CDATA #REQUIRED>
]>
<tutorials type="web">
<tutorial>
<name>XML Tutorial</name>
<url>http://www.jkcc.com/xml/tutorial</url>
</tutorial>
<tutorial>
<name>HTML Tutorial</name>
<url>http://www.jkcc.com/html/tutorial</url>
</tutorial>
</tutorials>
In the above code the keyword SYSTEM is used, which indicates that the code is executed on a
local system. If the keyword PUBLIC is used it indicates that the code is accessible globally.
This is the same XML document as above, but with an external DTD.
Example:
<?xml version="1.0"?
>
<!DOCTYPE
Item* The item appears 0 or more times. tutorials SYSTEM
Item+ The item appears 1 or more tiles. "tutorials.dtd">
<tutorials
Item? The item appears 0 or 1. type="web">
(Item1|item2) Set of alternatives. <tutorial>
6
<name>XML Tutorial</name>
<url>http://www.ssit.ac.in/xml/tutorial</url>
</tutorial>
<tutorial>
<name>HTML Tutorial</name>
<url>http://www.ssit.ac.in/html/tutorial</url>
</tutorial>
</tutorials>
DTD entities:
Entities are used as containers which will be filled with some form of content. The content may
be included in the XML file, an internal entity, or stored in another file, an external entity. As
with attributes and elements, entities may be either parsed or non-parsed. All complex data items
which do not need to pass through the XML parser should be defined as non-parsed.
There are two types of entities. They are:
1) Internal Entities.
2) External Entities.
1) Internal Entities:
Internal entities associate a name with a string of literal text. They allow us to
define shortcuts for frequently typed text or text that is expected to change.
The XML specification predefined internal entities are
< produces the left angle bracket <
> produces the right angle bracket >
& produces the ampersand &
' produces a single quote character (an apostrophe) ‘
" produces a double quote character “
© produces copyright symbol ©
User defined entities :
Syntax : <! ENTITY entity-name “entity-value”>
DTD example:
<!ENTITY writer "harish">
<! ENTITY pos “pinch of salt”>
XML example:
<author >&writer;©</author>
<item> Finally add &pos;</item>
2) External entities:
7
External entities associate a name with the content of another file. They allow
an XML document to refer to the contents of another file.
DTD example:
<! ENTITY writer SYSTEM “mydocument.xml”>
The writer entity is defined in mydocument.xml file.
<! ENTITY writer PUBLIC “http://www.xml.com/entities/entities.xml”>
XSL can be used to define how an XML file should be displayed by transforming the
XML file into a format that is recognizable to a browser. Once such format is HTML. Normally
XSL does this by transforming each XML element into an HTML element.
XSL can also add completely new elements into the output file, or remove elements. It
can rearrange and sort the elements, test and make decisions about which element to display, and
a lot more.
There are various elements that can be used with XSL.
Element Description
XML also has it's own styles language - XSL. XSL stands for Extensible Styles
Language and is a very powerful language for applying styles to XML documents. XSL has two
parts — a formatting language and a transformation language.
The formatting language allows you to apply styles similar to what CSS does. Browser
support for the XSL formatting language is limited at this stage.
The transformation language is known as XSLT (XSL Transformations). XSLT allows
you to transform your XML document into another form. For example, you could use XSLT to
dynamically output some (or all) of the contents of your XML file into an HTML document
containing other content.
Example:
sri.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="URI:xsl">
<xsl:template>
<html>
<body>
<table border="1" bgcolor="yellow">
<tr>
<th>name</th>
<th>pnone no</th>
<th>Email id</th>
</tr>
<xsl:for-each select="Contacts/contact">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="phoneno"/></td>
<td><xsl:value-of select="emailid"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
mydata.xml
9
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sri.xsl"?>
<Contacts>
<contact>
<name>rajesh</name>
<phoneno>123456</phoneno>
<emailid>[email protected]</emailid>
</contact>
</Contacts>
<contact>
<name> Anand</name>
<phoneno> 891011</phoneno>
<emailid>[email protected]</emailid>
</contact>
</Contacts>
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or
<xsl:transform>
The XSL style sheet is linked to the XML document using the following statement: <?
xml:stylesheet type="text/xsl" href="sri.xsl"?>
An XSL style sheet consists of one or more set of rules called templates. Each template contains
rules to apply when a specific node is matched. The <xsl:template> element is used to create
templates.
The <xsl:value of> element is used to extract the value of an XML element and add it to the
output stream of transformation.
The <xsl:for-each> element allows to select every XML element of specified node-set.
Formatting XML using XSL means that you need a complex processor to get any meaningful
output. Generally, at the moment, we are converting XML into HTML. Some web browsers are
able to parse XML, although those parsing capabilities may quite limit.
We used CSS to apply styles to the contents of our XML document. Applying styles improved
the look of our document when viewing it with a browser. The formatting language allows you
to apply styles similar to what CSS does. Browser support for the XSL formatting language is
limited at this stage.
<BOOK>
<TITLE>LET US ‘C’</TITLE>
<AUTHOR>Yeswanth Kanitkar</AUTHOR>
<COUNTRY>INDIA</COUNTRY>
<PUBLISHER >TATA MAGRILL</PUBLISHER >
<PRICE>325RS</PRICE>
<YEAR>1975</YEAR>
</BOOK>
</CATALOG>
abc.css:
CATALOG
{
background-color:yellow;
}
BOOK
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
TITLE
{
color:red;
font-size: 20pt;
}
AUTHOR
{
color:brown;
font-size: 20pt;
}
COUNTRY,PRICE,YEAR,PUBLISHER
{
display: block;
color:green;
margin-left: 20pt;
}
Web Services
11
A web service is a software module that is intended to carry out a specific set of
functions.
Web services in cloud computing can be found and invoked over the network.
The web service would be able to deliver functionality to the client that invoked the web
service.
A web service is a set of open protocols and standards that allow data to be exchanged
between different applications or systems.
Web services can be used by software programs written in a variety of programming
languages and running on a variety of platforms to exchange data via computer networks
such as the Internet in a similar way to inter-process communication on a single
computer.
Any software, application, or cloud technology that uses standardized web protocols
(HTTP or HTTPS) to connect, interoperate, and exchange data messages – commonly
XML (Extensible Markup Language) – across the internet is considered a web service.
Web services have the advantage of allowing programs developed in different languages
to connect with one another by exchanging data over a web service between clients and
servers.
A client invokes a web service by submitting an XML request, which the service
responds with an XML response.
Functions of Web Services
It’s possible to access it via the internet or intranet networks.
XML messaging protocol that is standardized.
Operating system or programming language independent.
Using the XML standard, it is self-describing.
A simple location approach can be used to locate it.
Components of Web Service
XML and HTTP is the most fundamental web services platform. The following components are
used by all typical web services:
SOAP (Simple Object Access Protocol)
SOAP stands for “Simple Object Access Protocol.”
It is a transport-independent messaging protocol. SOAP is built on sending XML data in
the form of SOAP Messages.
A document known as an XML document is attached to each message. Only the structure
of the XML document, not the content, follows a pattern.
The best thing about Web services and SOAP is that everything is sent through HTTP,
the standard web protocol.
A root element known as the element is required in every SOAP document. In an XML
document, the root element is the first element.
The “envelope” is separated into two halves. The header comes first, followed by the
body.
The routing data, or information that directs the XML document to which client it should
be sent to, is contained in the header. The real message will be in the body.
UDDI (Universal Description, Discovery, and Integration)
UDDI is a standard for specifying, publishing and discovering a service provider’s online
services.
It provides a specification that aids in the hosting of data via web services.
UDDI provides a repository where WSDL files can be hosted so that a client application
can discover a WSDL file to learn about the various actions that a web service offers.
As a result, the client application will have full access to the UDDI, which serves as a
database for all WSDL files.
12
The UDDI registry will hold the required information for the online service, just like a
telephone directory has the name, address, and phone number of a certain individual. So
that a client application may figure out where it is.
WSDL (Web Services Description Language)
If a web service can’t be found, it can’t be used. The client invoking the web service
should be aware of the location of the web service. Second, the client application must
understand what the web service does in order to invoke the correct web service. The
WSDL, or Web services description language, is used to accomplish this.
The WSDL file is another XML-based file that explains what the web service does to the
client application.
The client application will be able to understand where the web service is located and
how to use it by using the WSDL document.
How Does Web Service Work?
The diagram depicts a very simplified version of how a web service would function. The client
would use requests to send a sequence of web service calls to a server that would host the actual
web service.
Remote procedure calls are what are used to make these requests. Calls to methods hosted by the
relevant web service are known as Remote Procedure Calls (RPC). Example: Flipkart offers a
web service that displays prices for items offered on Flipkart.com. The front end or presentation
layer can be written in .Net or Java, but the web service can be communicated using either
programming language.
The data that is exchanged between the client and the server, which is XML, is the most
important part of a web service design. XML (Extensible markup language) is a simple
intermediate language that is understood by various programming languages. It is a counterpart
to HTML. As a result, when programs communicate with one another, they do so using XML.
This creates a common platform for applications written in different programming languages to
communicate with one another.
For transmitting XML data between applications, web services employ SOAP (Simple Object
Access Protocol). The data is sent using standard HTTP. A SOAP message is data that is sent
from the web service to the application. An XML document is all that is contained in a SOAP
message. The client application that calls the web service can be created in any programming
language because the content is written in XML.
Features/Characteristics Of Web Service
13
Web services have the following features:
(a) XML Based: The information representation and record transportation layers of a web service
employ XML. There is no need for networking, operating system, or platform binding when
using XML. At the middle level, web offering-based applications are highly interoperable.
(b) Loosely Coupled: A customer of an internet service provider isn’t necessarily directly linked
to that service provider. The user interface for a web service provider can change over time
without impacting the user’s ability to interact with the service provider. A strongly coupled
system means that the patron’s and server’s decisions are inextricably linked, indicating that if
one interface changes, the other should be updated as well.
A loosely connected architecture makes software systems more manageable and allows for easier
integration between different structures.
(c) Capability to be Synchronous or Asynchronous: Synchronicity refers to the client’s
connection to the function’s execution. The client is blocked and the client has to wait for the
service to complete its operation, before continuing in synchronous invocations. Asynchronous
operations allow a client to invoke a task and then continue with other tasks.
Asynchronous clients get their results later, but synchronous clients get their effect immediately
when the service is completed. The ability to enable loosely linked systems requires
asynchronous capabilities.
(d) Coarse-Grained: Object-oriented systems, such as Java, make their services available through
individual methods. At the corporate level, a character technique is far too fine an operation to be
useful. Building a Java application from the ground, necessitates the development of several
fine-grained strategies, which are then combined into a rough-grained provider that is consumed
by either a buyer or a service.
Corporations should be coarse-grained, as should the interfaces they expose. Web services
generation is an easy approach to define coarse-grained services that have access to enough
commercial enterprise logic.
(e) Supports Remote Procedural Call: Consumers can use an XML-based protocol to call
procedures, functions, and methods on remote objects utilizing web services. A web service must
support the input and output framework exposed by remote systems.
Enterprise-wide component development Over the last few years, JavaBeans (EJBs) and.NET
Components have become more prevalent in architectural and enterprise deployments. A number
of RPC techniques are used to allocate and access both technologies.
A web function can support RPC by offering its own services, similar to those of a traditional
role, or by translating incoming invocations into an EJB or.NET component invocation.
(f) Supports Document Exchanges: One of XML’s most appealing features is its simple approach
to communicating with data and complex entities. These records can be as simple as talking to a
current address or as complex as talking to an entire book or a Request for Quotation. Web
administrations facilitate the simple exchange of archives, which aids incorporate reconciliation.
The web benefit design can be seen in two ways: (i) The first step is to examine each web benefit
on-screen character in detail. (ii) The second is to take a look at the rapidly growing web benefit
convention stack.
15
3. At last we need to package the web service classes to the WAR file and deploy it to a
web server.
Examples of Java Web Services
This is an example of how we can use JAX-WS to create a Java Web Service (JWS) using the
data from the search results.
1. The SEI (Service Endpoint Interface), which shows the procedures of web services
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
import javax.jws.WebService;
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}
3. Annotate the SEI and it's implementation class with JAX-WX annotations to specify
the web services.
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
import javax.jws.WebService;
@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}
Web resources
16
Web resources are any software artifacts that the web application requires for proper rendering,
including images, script files, and any user-created component libraries. Resources must be
collected in a standard location, which can be one of the following.
A resource packaged in the web application root must be in a subdirectory of
a resources directory at the web application root: resources/resource-identifier.
A resource packaged in the web application's classpath must be in a subdirectory of
the META-INF/resources directory within a web
application: META-INF/resources/resource-identifier. You can use this file structure to
package resources in a JAR file bundled in the web application.
The Java Server Faces runtime will look for the resources in the preceding listed
locations, in that order.
Resource identifiers are unique strings that conform to the following format (all on one
line):[locale-prefix/][library-name/][library-version/]resource-name[/resource-version]
Elements of the resource identifier in brackets ([]) are optional, indicating that only
a resource-name, which is usually a file name, is a required element.
For example, the most common way to specify a style sheet, image, or script is to use
the library and name attributes, as in the following tag from the guessnumber-
jsf example:
<h:outputStylesheet library="css" name="default.css"/>
This tag specifies that the default.css style sheet is in the directory web/resources/css.
You can also specify the location of an image using the following syntax, also from
the guessnumber-jsf example:
<h:graphicImage value="#{resource['images:wave.med.gif']}"/>
This tag specifies that the image named wave.med.gif is in the
directory web/resources/images.
Resources can be considered as a library location. Any artifact, such as a composite
component or a template that is stored in the resources directory, becomes accessible to
the other application components, which can use it to create a resource instance.
17