37
Introduction to XML and Web Service ISYS 512/812

Introduction to XML and Web Service ISYS 512/812

Embed Size (px)

Citation preview

Page 1: Introduction to XML and Web Service ISYS 512/812

Introduction to XML and Web Service

ISYS 512/812

Page 2: Introduction to XML and Web Service ISYS 512/812

HTML vs XML

• HTML is a language specifically designed for displaying information in browser. It doesn’t carry information about the structure of the content held within its page.

• XML (Extensible MarkUp Language) is a language used to represent data in a form that does not rely on any particular proprietary technology. It uses user-defined tags to give meaningful names to data elements. With XML, we separate the data content from the data presentation.

Page 3: Introduction to XML and Web Service ISYS 512/812

HTML

<body>

<h1>My Favorite Book</h1>

<h1>ISBN - 1-34567-04-01</h1>

<H1>Authors:</H1>

<h3>John Smith</h1>

<h3>Peter Chen</h1>

<h1>Price: $45.00</h1>

<h1>Description: This is a grerat book</h1>

</body>

Page 4: Introduction to XML and Web Service ISYS 512/812

XML<?xml version="1.0" ?>

<Books>

<Book>

<ISBN>1-34567-04-01</ISBN>

<Authors>

<AuthorName>John Smith</AuthorName>

<AuthorName>Peter Chen</AuthorName>

<AuthorName>David Chao</AuthorName>

</Authors>

<Price> $45.00</Price>

<Description>This is a grerat book</Description>

</Book>

<Book>

<ISBN>1-34567-04-02</ISBN>

<Authors>

<AuthorName>Adam Smith</AuthorName>

</Authors>

<Price> $25.00</Price>

<Description>This is a second great book</Description>

</Book>

</Books>

Page 5: Introduction to XML and Web Service ISYS 512/812

XML Document

• Well-formed documents:– Documents comply with the rules of XML

syntax.

• Valid documents:– Documents that are well-formed and comply

with a XML schema or Document Type Definition (DTD is a file containing the rules used to define the document).

Page 6: Introduction to XML and Web Service ISYS 512/812

Well-Formed Documents

• There is only one root node.• All elements must have a start tag and an end tag

(except for an empty element, see example below) Elements must be nested correctly.

• If an element has an attribute, the attribute value must be quoted.– An empty element with an attribute:

• <price US=“$49” />

• Note: XML is case sensitive.

Page 7: Introduction to XML and Web Service ISYS 512/812

XML Documents• The XML declaration:

– <?xml version=“1.0” ?>– Note: xml should be in lower case.

• Namespaces– Define a namespace: xmlns:”uri” where the Uniform

Resource Identifier is a unique identifier and is often a URL.– Use a namespace to make a tag unique:

• <namespace: TagName>

• Elements: – Each start-tag/end-tag pair, with the data that lies between

them, is an element.– Ex. <AuthorName>John Smith</AuthorName>– The start and end tag must be in the same case.

Page 8: Introduction to XML and Web Service ISYS 512/812

• Attributes: Elements can have attributes. These are values that are passed to the application, but not constitute part of the content of the element. Attributes are included as part of the element’s start tag, and values must be enclosed in quote marks.– <Food healthy=“yes”>Spinach</Food>

– <Food healthy=“yes” tasty=“no”>Carrot</Food>

Page 9: Introduction to XML and Web Service ISYS 512/812

• Comments:– <!--This is a comment -->

• Processing instructions: These allow documents to contain instructions for applications using XML data.– <?NameOfTargetApplication Instructions for

application?>

Page 10: Introduction to XML and Web Service ISYS 512/812

Tree View of XML

Books

Book

ISBN Authors Price Description

Book

ISBN Authors Price Description

Author Author Author

Page 11: Introduction to XML and Web Service ISYS 512/812

Valid Documents

• Well-formed documents that conform to an XML schema.

• XML parsers are programs that are able to validate an XML document against schema.

Page 12: Introduction to XML and Web Service ISYS 512/812

XML SchemaDefinition of an XML Document

• Namespaces: Allow element names to be qualified to avoid name collisions.

• Complex and simple types:– Elements that contains other elements are complex type.

• Cardinality: – minOccurs: 0 for optional element.– maxOccurs: specified number or unbounded

• Compositor:– Sequence: defines an ordered sequence of subelements.– Choice: defines a choice between several possible elements.

• Constraints:– Uniqueness contraint

Page 13: Introduction to XML and Web Service ISYS 512/812

VS.Net Tools for XML Document and Schema

• XML Schema:– Project/Add New Item/XML Schema

• Drag tables from Server Explorer

• One table

• Two tables with relation

• View Code

• XML File:– Project/Add New Item/XML File

• Add tags

Page 14: Introduction to XML and Web Service ISYS 512/812

Creating XML File<?xml version="1.0" encoding="utf-8"?><books> <book> <author>Chao </author> <isbn>12345</isbn> <price>45</price> </book> <book> <author>Smith</author> <isbn>3456</isbn> <price>50</price> </book> <book> <author>Chen</author> <isbn>3445</isbn> <price>56</price> </book></books>

Page 15: Introduction to XML and Web Service ISYS 512/812

Read XML File• DataSet object’s ReadXML method.

– Add a new dataset object:• Project/Add New Item/DataSet

• Use the ReadXML method to read XML file into the dataset.

Dim MyDs As New DataSet2MyDs.ReadXml("C:\VBExamplesVS05\TestXML\TestXML\XMLFile1.xml", XmlReadMode.InferSchema)Dim objDataView As New DataView(MyDs.Tables("Book"))

DataGridView1.DataSource = objDataView

Page 16: Introduction to XML and Web Service ISYS 512/812

Write XML Schema

Dim MyDs As New DataSet2MyDs.ReadXml("C:\VBExamplesVS05\TestXML\TestXML\

XMLFile1.xml", XmlReadMode.InferSchema)

MyDs.WriteXmlSchema("c:\MyDsSchema.txt")

MyDs.WriteXml("c:\MyDsXML.txt")

Page 17: Introduction to XML and Web Service ISYS 512/812

Style Sheets

• XSL: Extensible Stylesheet Language

• Tell the browser how to display the XML document.

• With a style sheet, all of the style rules are kept in one file and the source document simply links to this file.

• File extension: .CSS

Page 18: Introduction to XML and Web Service ISYS 512/812

• Browser screen structure:– Page

• Area– Block Flow Object: Taking up the whole line.

– In-Line Flow Object: Several in-line objects may be on the same line.

Page 19: Introduction to XML and Web Service ISYS 512/812

<?xml version="1.0" ?>

<?xml:stylesheet href="books.css" type="text/css" ?><Books><Book><btitle>My Favorite Book</btitle><ISBN>1-34567-04-01</ISBN>

<Authors><AuthorName>John Smith</AuthorName><AuthorName>Peter Chen</AuthorName>

</Authors><Price> $45.00</Price><Description>This is a grerat book</Description>

</Book><Book><btitle>My Second Favorite Book</btitle><ISBN>1-34567-04-02</ISBN>

<Authors>Adam Smith</Authors><Price> $25.00</Price><Description>This is a second great book</Description>

</Book></Books>

Page 20: Introduction to XML and Web Service ISYS 512/812

btitle {display:block;font-family: Aerial, Helvetica;font-weight: bold;font-size: 20pt;color: #9370db;text-align: center;}

ISBN {display:block;font-family: Aerial, Helvetica;font-weight: bold;font-size: 12pt;color: #c71585;text-align: left;}

Authors {display:inline;font-family: Aerial, Helvetica;font-style: italic;font-size: 10pt;color: #9370db;text-align: left;}

Page 21: Introduction to XML and Web Service ISYS 512/812

Price {

display:block;

font-family: Aerial, Helvetica;

font-size: 12pt;

color: #ff1010;

text-align: left;

}

Description {

display:block;

font-family: Aerial, Helvetica;

font-size: 12pt;

color: #ff1010;

text-align: left;

}

Page 22: Introduction to XML and Web Service ISYS 512/812

Demo

• ASPNet/FavoriteBook.htm

Page 23: Introduction to XML and Web Service ISYS 512/812

Web Service

• XML Web Service

• Web services are classes that are stored on the web which can instantiate and use in both Windows and Web applications.

Page 24: Introduction to XML and Web Service ISYS 512/812

To Add a New Web Service:

• Website/Add New Item/Web Service– Web Service has an extension: ASMX– The CodeBehind File is stored in the

App_Code folder.

• Web Services are defined as Web Method:– <WebMethod()> _

Page 25: Introduction to XML and Web Service ISYS 512/812

A Web Service Example

Public Class MyWebService Inherits System.Web.Services.WebService <WebMethod()> Public Function GetCname(ByVal CID As String) As String Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where CID = '" & CID & "';" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read() Then Return objDataReader("Cname") Else Return ("Not exist") End If End FunctionEnd Class

Page 26: Introduction to XML and Web Service ISYS 512/812

Web Service Description Language (WSDL)

• A WSDL file is an XML document containing a complete description of the web service. It shows a web service’s name, methods, and parameter types.

• Help page: After entering web service’s URL, a help page is displayed. You can click the Service Description link to see the WSDL file.

Page 27: Introduction to XML and Web Service ISYS 512/812

Returning a Complex Data Type

• Examples:– ArrayList– DataSet– User-defined object

• Customer Class

Page 28: Introduction to XML and Web Service ISYS 512/812

Returning an ArrayList <WebMethod()> Public Function GetCnameRating(ByVal CID As String) As ArrayList Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where CID = '" & CID & "';" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Dim temp As New ArrayList If objDataReader.Read() Then temp.Add(objDataReader("Cname")) temp.Add(objDataReader("Rating")) Else temp.Add("Not exist") End If Return temp End Function

Page 29: Introduction to XML and Web Service ISYS 512/812

Returning a DataSet

<WebMethod()> Public Function GetCustomerDataSet() As DataSet Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLSelect As String = "select * from customer;" Dim objDataSet As New DataSet() Dim objAdapter As New OleDbDataAdapter(strSQLSelect, objConn) objAdapter.Fill(objDataSet, "Customer") Return objDataSet End Function

Page 30: Introduction to XML and Web Service ISYS 512/812

Consuming Web Services from a Windows Application

• Add a web reference to the web service:– Project/Add Web Reference

• Local computer

• Imports the web service• Declare a web service class variable.

– Dim UseWs As New DBWebService

• Note: Web Service CodeBehind file location.

Page 31: Introduction to XML and Web Service ISYS 512/812

Code Example (Windows Project)

Imports TestXML.MyWServicePublic Class Form3 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyWs As New MyWebService Try MessageBox.Show(MyWs.GetCname(InputBox("enter cid: ")))

Catch except As SystemException MessageBox.Show(except.Message) End Try End SubEnd Class

Page 32: Introduction to XML and Web Service ISYS 512/812

Using a DataSet Returning by Web Service(Web Project)

Imports DBWSImports System.DataPartial Class _DefaultInherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim UseWs As New DBWebService Dim MyDs As New dataset MyDs = UseWs.GetCustomerDataSet() GridView1.DataSource = MyDs.Tables("customer") GridView1.DataBind() End SubEnd Class

Page 33: Introduction to XML and Web Service ISYS 512/812

Reference a Web Service on Internet

• Mortgage calculator

• http://www.webservicex.net/mortgage.asmx

Page 34: Introduction to XML and Web Service ISYS 512/812

Universal Description, Discovery, and Integration (UDDI)

• A directory service for web services.– http://uddi.org

• UDDI is an open industry initiative enabling businesses to publish service listings and discover each other and define how the services or software applications interact over the Internet. A UDDI business registration consists of three components:– White Pages — address, contact, and known identifiers; – Yellow Pages — industrial categorizations based on standard

taxonomies; and – Green Pages — technical information about services exposed by

the business.

Page 35: Introduction to XML and Web Service ISYS 512/812

Loosely Coupled Software

• Refers to software routines (modules, programs) that can be called by an application and executed as needed. For example, Web services are loosely coupled software modules that are invoked on demand when required.

• An approach to designing interfaces across modules to reduce the interdependencies across modules or components – in particular, reducing the risk that changes within one module will create unanticipated changes within other modules.

Page 36: Introduction to XML and Web Service ISYS 512/812

Benefits of Loosely Coupled Software

• This approach specifically seeks to increase flexibility in adding modules, replacing modules and changing operations within individual modules.

• It assumes a modular approach to design.  • It values flexibility. • It seeks to increase flexibility by focusing

on design of interfaces.

Page 37: Introduction to XML and Web Service ISYS 512/812

Service Oriented Architecture

• SOA expresses a perspective of software architecture that defines the use of loosely coupled software services to support the requirements of the business processes and software users. In an SOA environment, resources on a network are made available as independent services that can be accessed without knowledge of their underlying platform implementation.

• A service-oriented architecture is not tied to a specific technology. It may be implemented using a wide range of technologies.