Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

SAVEETHA SCHOOL OF ENGINEERING

SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES

Department of Computer Science and Engineering

ASSIGNMENT II

Name & Register No : B. SATISH

Year & Sem : 191711190

Slot : III

Subject Code & Name : CSE2802 & XML and Web Services - WSDL

Topic : Create A Simple XML Document To Display The Address Book

Submitted to :Dr.Shri Vindhya

Mark
Marks
Content Details Split
Awarded
up
Aim (10) Suitable to title 10
Steps wise Description 10
Algorithm (20)
Logic Description 10
Version Declaration 5
Root Element 5
Sub Element 5
Number of child element 5
Program (40)
Number of attributes 5
Proper nesting 5
Element declaration 5
Attribute declaration 5
Opening element 5
Closing element 5
Concept (20) Application of xml 5
program
Attribute explanation 5
Output(10) Actual Output 10
Total = 100

Faculty HOD

191711190 || B.SATISH
A Simple XML Document To Display The Address Book

AIM:

To Create A Simple Xml Document To Display The Address Book.

What is an Address Book?

An Address Book is the place where you store the necessary contact informations,
email addresses of the contacts that you wish to be in regular touch with. It helps
make sending emails more convenient. Addresses can be stored as Individuals and/or
Groups.

How can I use the Address Book?

➢ To use your address book, you need to first add name, email,phone numbar,
addresses etc of your contacts (family, friends, relatives etc.). Here I am
going to discuss how to create a address book in simple way by using XML
file and how to create XML file at run time.
➢ XML is a file extension for an Extensible Markup Language (XML) file
format used to create common information formats and share both the format
and the data on the World Wide Web, intranets, and elsewhere using standard
ASCII text.

➢ XML is similar to HTML. Both XML and HTML contain markup symbols
to describe the contents of a page or file. HTML, however, describes the
content of a Web page (mainly text and graphic images) only in terms of how
it is to be displayed and interacted with. For example, the letter "p" placed
within markup tags starts a new paragraph.

191711190 || B.SATISH
➢ XML describes the content in terms of what data is being described. For
example, the word "phonenum" placed within markup tags could indicate that
the data that followed was a phone number. An XML file can be processed
purely as data by a program or it can be stored with similar data on another
computer or it can be displayed, like an HTML file. For example, depending
on how the application in the receiving computer wanted to handle the phone
number, it could be stored, displayed, or dialed.

➢ XML is considered extensible because, unlike HTML, the markup symbols


are unlimited and self-defining. XML is a simpler and easier-to-use subset of
the Standard Generalized Markup Language (SGML) standard for how to
create a document structure. It is expected that HTML and XML will be used
together in many Web applications. XML markup, for example, may appear
within an HTML page.

ALGORITHM :

To generate an XML file from a schema file follow these steps:

➢ In the Navigator view, right-click the XML schema file that you want to work
with and select Generate > XML File.
➢ Select a project or folder to contain the XML file and type a name for the file
in the File Name field, then click Next. The name of the file must end in .xml.
➢ Select the Root element of the XML file. The root element of an XML file is
the element that contains all other elements in that file. All of the global
elements you have defined in the XML schema will be included in the Root
element list. If you do not have any global elements in your XML schema,
you cannot create an XML file from it.

191711190 || B.SATISH
Logic Description and its steps:

➢ Create optional attributes: both mandatory and optional attributes will be


generated.
➢ Create optional elements: both mandatory and optional elements will be
generated.
➢ Create first choice of required choice: the first option of a required choice will
be generated in your XML file.
➢ Fill elements and attributes with data: any elements and attributes generated
will be filled with sample data.
➢ If you do not select any of these options, then only the minimum amount of
content required for the XML file will be created.
➢ The Namespace Information section contains information about the target
namespace of the XML schema, its prefix, and the schema location. Select the
entry you want to edit and click Edit.
➢ The value in the Namespace Name field is the target namespace of the XML
schema. Your XML file should be associated with the same namespace as its
XML schema is associated with.
➢ All qualified elements and attributes in the XML file associated with the
namespace will be prefixed with the Prefix value.
➢ The Location Hint field contains the location of the XML schema.
➢ Click OK and then Finish.
➢ The XML file will contain the selected root element and any elements or
attributes contained in the root element. It also contains information about the
XML file namespace and location. You can now add elements and attributes
to the XML file.

191711190 || B.SATISH
PROGRAM :

XML file:

Version

<?xml version="1.0" encoding="utf-8"?>

<guestbook>

<entryname="Purushottamrathore" email="puru.rathore@gmail.com" location="Delhi"


ContactNo="9858784585" date="1/19/2009 5:36:21 PM">Hi this is purushottam rathore from
Delhi (India)</entry>

<entry name="Hello" email="hrrr@yahoo.com" location="Hi" ContactNo="9958695874"


date="1/16/2009 4:28:10 PM">Hello everybody...</entry>

<entry name="Dharmendra" email="dharmendra123@gmail.com" location="Noida"


ContactNo="9685487584" date="1/16/2009 3:40:26 PM">Hi this is dharmendra.Hi this is
dharmendra.Hi this is dharmendra.Hi this is dharmendra.Hi this is dharmendra.Hi this is
dharmendra.</entry>

<entry name="smith" email="smith.dk@hotmail.com" location="United State"


ContactNo="9878458574" date="1/16/2009 3:25:28 PM">Hi this is Smith from US</entry>

<entry name="purushottam" email="puru_rock@gmail.com" location="Delhi"


ContactNo="9586784521" date="1/16/2009 2:36:23 PM">Hi this is puru.</entry>

</guestbook>---------Root Element

191711190 || B.SATISH
Version:

<?xml version="1.0" encoding="utf-8"?>

Root Element:

<guest book>………………</guest book>

Sub Elements:

1.<entry name>……..</entry>

2.<contact Details>

i)name

ii)email

iii)phone number etc…

Number of Attributes:

1.Name

2.location

3.email

4.contact

5.comments

Nesting

Nesting is done in the entire program under the root element ,the sub elements are
declared under the sub elements many childs elements are declared this is known as
nesting.

191711190 || B.SATISH
Xml element declaration:

<?xml version = "1.0"?>


<contact-info>
<address category = "residence">
<name>satish barla</name>
<company>tech mahindra</company>
<phone>(011) 123-4567</phone>
</address>
</contact-info>

Xml attributes declaration:

<Person xmlns='urn:w3.org:People'
xmlns:b='urn: w3.org:People:base'
xmlns:u='urn: w3.org:units' >
<name>satish</name>
<age b:base='10' u:units='years' >20</age>
</Person>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

191711190 || B.SATISH
<title>Guestbook</title>

<%--JavaScript for display message.--%>

<script type="text/javascript">

function disp_alert()

alert("Sorry! This is test application. You cann't send mail from here.");

</script>

</head>

<body>

<fieldset align="middle">

<legend><b>Guestbook</b></legend>

<form id="form1" runat="server">

<table width="100%" cellpadding="2" cellspacing="2">

<tr height="20" bgcolor="maroon">

<td colspan="2">

<font color="Silver">Guestbook</font>

</td>

</tr>

191711190 || B.SATISH
<tr>

<td width="30%" valign="top">

<table bgcolor="silver" width="100%" cellpadding="2" cellspacing="3"


style="border-right:

red 1px solid;

border-top: red 1px solid; border-bottom: red 1px solid; border-left: red 1px solid;">

<tr>

<td colspan="2">

<asp:Label ID="ErrorMessage" runat="server" Font-Names="arial" Font-


Size="14px" ForeColor="red"

Visible="false"></asp:Label></td>

</tr>

<tr>

<td style="width: 100px">

Name:</td>

<td style="width: 100px">

<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

191711190 || B.SATISH
Location:</td>

<td style="width: 100px">

<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

Email:</td>

<td style="width: 100px">

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

ContactNo:</td>

<td style="width: 100px">

<asp:TextBoxID="TextBoxContact"runat="server"
MaxLength="10"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

191711190 || B.SATISH
Comments:</td>

<td style="width: 100px">

<asp:TextBox ID="txtComments" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td>

</td>

<td>

<asp:Button ID="ButtonSubmite" runat="server" OnClick="Submit_Click"


Text="Submit" />

<asp:Button ID="ButtonReset" runat="server" OnClick="Reset_Click"


Text="Reset" />

</td>

</tr>

</table>

</td>

<td width="70%">

<asp:DataList ID="Guestbook" runat="server" Width="100%"


AlternatingItemStyle-BackColor="lightgray"

BorderColor="red" BorderWidth="1pt" CellPadding="5" CellSpacing="5">

191711190 || B.SATISH
<ItemTemplate>

<font color="#660066">Name:

<%# DataBinder.Eval(Container.DataItem, "name") %>

</font>

<br />

E-mail: <a href="#" onclick="disp_alert()">

<%# DataBinder.Eval(Container.DataItem, "email") %>

</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Contact No:

<%# DataBinder.Eval(Container.DataItem, "ContactNo") %>

<br />

<font color="#660066">Location:

<%# DataBinder.Eval(Container.DataItem, "location") %>

</font>

<br />

<font color="#330000"">Date:

<%# DataBinder.Eval(Container.DataItem, "date") %>

</font>

<br />

191711190 || B.SATISH
<font color="#660066">About&nbsp;<%# DataBinder.Eval(Container.DataItem,
"name") :</font><font color="black" face="Arial" size="2pt">

<%# DataBinder.Eval(Container.DataItem, "entry_Text") %>

</font>

</ItemTemplate>

<SeparatorTemplate>

<tr height="1px" bgcolor="red">

<td>

</td>

</tr>

</SeparatorTemplate>

</asp:DataList>

</td>

</tr>

</table>

</form>

</fieldset>

</body>

</html>

191711190 || B.SATISH
Default.aspx.cs:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Xml;

public partial class _Default : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

BindData();

191711190 || B.SATISH
void BindData()

XmlTextReadermyXmlReader=new
XmlTextReader(Server.MapPath("guestbook.xml"));

DataSet myDataSet = new DataSet();

myDataSet.ReadXml(myXmlReader);

myXmlReader.Close();

Guestbook.DataSource = myDataSet.Tables[0];

Guestbook.DataBind();

protected void Submit_Click(object sender, EventArgs e)

if (txtName.Text == "" || txtEmail.Text == "" || txtLocation.Text == "" ||


txtComments.Text == "")

ErrorMessage.Text = "Please fill all entries.";

ErrorMessage.Visible = true;

else

191711190 || B.SATISH
// Open the XML doc

System.Xml.XmlDocumentmyXmlDocument = new System.Xml.XmlDocument();

myXmlDocument.Load(Server.MapPath("guestbook.xml"));

System.Xml.XmlNodmyXmlNode=
myXmlDocument.DocumentElement.FirstChild;

// Create new XML element and populate its attributes

System.Xml.XmlElementmyXmlElement=
myXmlDocument.CreateElement("entry");

myXmlElement.SetAttribute("name", Server.HtmlEncode(txtName.Text));

myXmlElement.SetAttribute("email", Server.HtmlEncode(txtEmail.Text));

myXmlElement.SetAttribute("location", Server.HtmlEncode(txtLocation.Text));

myXmlElement.SetAttribute("ContactNo",
Server.HtmlEncode(TextBoxContact.Text));

myXmlElement.SetAttribute("date", DateTime.Now.ToString());

myXmlElement.InnerText = Server.HtmlEncode(txtComments.Text);

// Insert data into the XML doc and save

myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);

myXmlDocument.Save(Server.MapPath("guestbook.xml"));

txtName.Text = "";

txtEmail.Text = "";

191711190 || B.SATISH
txtLocation.Text = "";

txtComments.Text = "";

TextBoxContact.Text = "";

BindData();}

protected void Reset_Click(object sender, EventArgs e)

txtName.Text = "";

txtEmail.Text = "";

txtLocation.Text = "";

txtComments.Text = "";

TextBoxContact.Text = "";

191711190 || B.SATISH
CONCEPT:

<!DOCTYPE RootElement (SYSTEM | PUBLIC)

ExternalDeclarations? [InternalDeclarations]? >

XML Document Structure:

➢ The XML Recommendation states that an XML document has both logical
and physical structure. Physically, it is comprised of storage units called
entities, each of which may refer to other entities, similar to the way that
include works in the C language. Logically, an XML document consists of
declarations, elements, comments, character references, and processing
instructions, collectively known as the markup.
➢ An XML document consists of three parts, in the order given:
1.An XML declaration (which is technically optional, but recommended in
most normal cases)
2.A document type declaration that refers to a DTD (which is optional, but
required if you want validation)
3. A body or document instance (which is required)

Collectively, the XML declaration and the document type declaration are called the
XML prolog.

XML Declaration:

The XML declaration is a piece of markup (which may span multiple lines of a file)
that identifies this as an XML document. The declaration also indicates whether the
document can be validated by referring to an external Document Type Definition
(DTD). DTDs are the subject of chapter 4; for now, just think of a DTD as a set of
rules that describes the structure of an XML document.

191711190 || B.SATISH
The minimal XML declaration is:

<?xml version="1.0" ?>

XML is case-sensitive (more about this in the next subsection), so it's important that
you use lowercase for xml and version. The quotes around the value of the version
attribute are required, as are the ? characters. At the time of this writing, "1.0" is the
only acceptable value for the version attribute, but this is certain to change when a
subsequent version of the XML specification appears.

NOTE:

Do not include a space before the string xml or between the question mark and the
angle brackets. The strings <?xml and ?> must appear exactly as indicated. The
space before the ?> is optional. No blank lines or space may precede the XML
declaration; adding white space here can produce strange error messages.

➢ In most cases, this XML declaration is present. If so, it must be the very first
line of the document and must not have leading white space. This declaration
is technically optional; cases where it may be omitted include when
combining XML storage units to create a larger, composite document.
➢ This Extended Backus-Naur Form (EBNF) notation, characteristic of many
W3C specifications, means that an XML declaration consists of the literal
sequence '<?xml', followed by the required version information, followed by
optional encoding and standalone declarations, followed by an optional
amount of white space, and terminating with the literal sequence '?>'. In this
notation, a question mark not contained in quotes means that the term that
precedes it is optional.

191711190 || B.SATISH
➢ The optional encoding part of the declaration tells the XML processor (parser)
how to interpret the bytes based on a particular character set. The default
encoding is UTF-8, which is one of seven character-encoding schemes used
by the Unicode standard, also used as the default for Java. In UTF-8, one byte
is used to represent the most common characters and three bytes are used for
the less common special characters. UTF-8 is an efficient form of Unicode for
ASCII-based documents. In fact, UTF-8 is a superset of ASCII.3

Document Type Declaration:

The document type declaration follows the XML declaration. The purpose of this
declaration is to announce the root element (sometimes called the document
element) and to provide the location of the DTD.

The general syntax is:

<!DOCTYPE RootElement (SYSTEM | PUBLIC)

ExternalDeclarations? [InternalDeclarations]? >

➢ where <!DOCTYPE is a literal string, RootElement is whatever you name the


outermost element of your hierarchy, followed by either the literal keyword
SYSTEM or PUBLIC. The optional ExternalDeclarations portion is typically
the relative path or URL to the DTD that describes your document type. (It is
really only optional if the entire DTD appears as an InternalDeclaration, which
is neither likely nor desirable.) If there are InternalDeclarations, they must be
enclosed in square brackets. In general, you'll encounter far more cases with
ExternalDeclarations than InternalDeclarations, so let's ignore the latter for
now.

191711190 || B.SATISH
➢ Let's start with a simple but common case. In this example, we are indicating
that the DTD and the XML document reside in the same directory (i.e., the
ExternalDeclarations are contained in the file employees.dtd) and that the root
element is Employees:

Document Body

➢ The document body, or instance, is the bulk of the information content of the
document. Whereas across multiple instances of a document of a given type
(as identified by the DOCTYPE) the XML prolog will remain constant, the
document body changes with each document instance (in general). This is
because the prolog defines (either directly or indirectly) the overall structure
while the body contains the real instance-specific data. Comparing this to data
structures in computer languages, the DTD referenced in the prolog is
analogous to a struct in the C language or a class definition in Java.

➢ The document body is analogous to a runtime instance of the struct or


class.Because the document type declaration specifies the root element, this
must be the first element the parser encounters. If any other element but the
one identified by the DOCTYPE line appears first, the document is
immediately invalid.

The DOCTYPE is "html" (not "xhtml"), so the document body begins with <html
....> and ends with </html>. Simple XHTML 1.0 Document with XML Prolog and
Document Body

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html

191711190 || B.SATISH
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>XHTML 1.0</title>

</head>

<body>

<h1>Simple XHTML 1.0 Example</h1>

<p>See the <a href=

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">DTD</a>.</p>

</body>

</html>

191711190 || B.SATISH
OUTPUT:

191711190 || B.SATISH

You might also like