20
Introduction to XML 1

Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

Embed Size (px)

Citation preview

Page 1: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

Introduction to XML

1

Page 2: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML

•XML started out as a standard data exchange format for the Web

•Yet, it has quickly become the fundamental instrument in the development of Web-based online information services and electronic commerce applications

•Today, many applications on the Web use XML for hosting and storing large amounts of structured data.

•XML standard is set and maintained by W3C (http://www.w3.org).

2

Page 3: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

What is XML?

•XML stands for eXtensible Markup Language.

•XML is a markup language much like HTML.

•XML was designed to describe data;

•XML tags are not predefined in XML. You must define your own tags.

•XML is a portable, and an open standard (i.e., nonproprietary or fee-free) technology for data storage and exchange.

•XML Provides the ability to describe data in an open text-based format and deliver it using standard http protocol.

3

Page 4: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

The main difference between XML and HTML

•XML is not a replacement for HTML.

•XML and HTML were designed with different goals.

•XML was designed to describe data and to focus on what data is.

•HTML was designed to display data and to focus on how data looks.

4

Page 5: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

Creating a simple XML Document

We want to create a XML document for the following information 

Name Kim

Home Address 1234 South Street, NY, USA

Job Title Vice President

Salary $175,000  

5

Page 6: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML Example

<EmployeeRecord> <Name> Kim </Name>

<HomeAddress> 34 South Street, NY, USA </HomeAddress> <JobTitle> Vice President </JobTitle> <Salary> $175,000 </Salary></EmployeeRecord>

•XML documents are stored as files with the .xml extension

•An XML document contains: Text that represents the data. Elements that specify its structure.

6

Page 7: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML Elements

•XML elements are similar to HTML elements, they consist of

Start tag <tagname> End tag </tagname> Content between the two tags

•Example:<Name> Kim </Name>

7

Page 8: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

Tagging Data in XML

<Salary> $175,000 </Salary>

•Considering the content only, it is not possible to understand what $175,000 stands for.

•The tag name Salary inform us that the content represents a salary.

•Similarly, an XML element might be tagged as gender, birth date, price,…

•XML is extensible, users create their own tags, the tag names are neither predefined nor limited.

•In HTML, the author of HTML documents can only use tags that are defined in the HTML standard (like <p> and <body>.....).

8

Page 9: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML – Element Nesting

•Elements may be nested to any depth to provide structured data

•The previous XML document is modified to include more elements under the <HomeAddress> element.

<EmployeeRecord> <Name> Kim </Name>

<HomeAddress> <Street> 34 South Street </Street> <State> NY </State> <Country> USA </Country>

</HomeAddress> <JobTitle> Vice President </JobTitle> <Salary> $175,000 </Salary></EmployeeRecord>  

9

Page 10: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML – Element Nesting

•Elements may be repeated to represent a list of values.

<ClassList><Student>

<Name> Ali</Name> <ID> 12345 </ID> </Student> <Student> …. </Student> . .

</ClassList> 10

<Student> element repeated to list all students in a class

Page 11: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

All XML elements must be properly nested

•Nesting XML tags improperly is a syntax error.

•In XML all elements must be properly nested within each other like this:

<Student> <Name> Ali </Name> </Student>

•In HTML some elements can be improperly nested within each other like this:

<b> <i> This text is bold and italic </b> </i>

NOTE: It is considered a bad coding practice to nest tags improperly in HTML.

11

Page 12: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML Root Element

•A valid XML document contains a single root element, which constitutes the top-level of nesting.

•The first tag in an XML document is the root tag.

•All the other elements must be nested within the root element.

<root><child>

<subchild> .....

</subchild></child>

</root> 12

<EmployeeRecord>

</EmployeeRecord>

Page 13: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML Element Names

•XML element names can be of any length and can contain letters, digits, underscores, hyphens and periods.

•XML element names must begin with either a letter or an underscore, and they should not begin with “xml” in any combination of uppercase and lowercase letters, as this is reserved for use in the XML standards.

•Using a white-space character in an XML element name is an error.

Examples of invalid element names:<xml_id> $175,000 </xml_id><1stPhone> $175,000 </1stPhone>

<First name> $175,000 </First name> 13

Page 14: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

All XML elements must have a closing tag

•In an XML document, each start tag must have a matchingend tag; omitting either tag is an error.

•In HTML some elements do not have to have a closing tag. The following code is legal in HTML:

<p> This is a paragraph <p> This is another paragraph

•In XML both tags must be included:

<Name> Ali</Name><ID> 12345 </ID>

  14

Page 15: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML tags are case sensitive

•Unlike HTML, XML tags are case sensitive.

•Using different cases for the start tag and end tag names for the same element is a syntax error.

•With XML, the tag <Letter> is different from the tag <letter>.

•Opening and closing tags must therefore be written with the same case:

<Letter> This is correct in XML</Letter> <letter> This is correct in XML</letter>

15

Page 16: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

White Space in Element

In XML, the white space in element is not truncated.

<Message> He is my friend</Message><Message> He is my friend</Message>

The above two XML statements are different.

16

Page 17: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

XML - Attributes

•XML elements can have attributes, just like HTML.

•Attributes provide additional information about an element.

Example: <salary> $175,000 </salary>

<salary currency = “USD”> 175,000 </salary>

17

Page 18: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

Attribute used as primary key

<catalog>     <book id=“bk101”>        <author> Mathew</author>   <title> XML Guide</title>   <price currency=“USD”>44.95</price>   </Book> <book id=“bk102”>        <author>Saleem</author>   <title>Java Handbook</title>   <price currency=“KWD”>12.00</price>   </book>  </catalog>

18

Page 19: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

Attribute Quotes

•With XML, it is illegal to omit quotation marks around attribute values. 

Incorrect attribute use:<price currency=KWD>12.00</price>

•Either single or double quotes can be used. 

<price currency=“KWD”>12.00</price><price currency=‘KWD’>12.00</price>

•If the attribute value itself contains double quotes you can use single quotes.

19

Page 20: Introduction to XML 1. XML XML started out as a standard data exchange format for the Web Yet, it has quickly become the fundamental instrument in the

White Space in Attribute

In XML, the white space in the attribute is truncated or ignored.

<student id=“isc 1234”>Ali</student>

<student id=“isc 1234”>Ali</student>

The above two XML statements are the same.

20