Sunteți pe pagina 1din 30

ATS Application Programming: Java

Programming

BB: XML

© Accenture 2005 All Rights Reserved Course Code #Z16325


Objectives
This review session will cover XML.

Included in this review:


 Introduction to XML

– What is XML?
– Why is XML important?
– How Can You Use XML?
 Generating XML Data
– Writing a Simple XML File
– Defining the Root Element
©2004 Accenture All Rights Reserved. 2
 DTD
© Accenture 2005 All Rights Reserved
INTRODUCTION TO XML
What is XML?

 XML stands for EXtensible Markup


Language.
 XML is a cross-platform, software and
hardware independent tool for transmitting
information.
 XML is a markup language much like
HTML.
 XML tags are not predefined in XML.
©2004 Accenture All Rights Reserved.
©2004 Accenture All Rights Reserved. 3
© Accenture 2005 All Rights Reserved
WHAT IS XML?
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.
©2004 Accenture All Rights Reserved. 4
© Accenture 2005 All Rights Reserved
WHAT IS XML?
XML is Free and Extensible

XML tags are not predefined. You must


"invent" your
own tags.

 The tags used to markup HTML documents


and the structures of HTML documents are
predefined. The author of HTML documents
can only use tags that are defined in the
©2004 Accenture All Rights Reserved. 5
HTML standard.
© Accenture 2005 All Rights Reserved
INTRODUCTION TO XML
What is XML? (Tags & Attributes)
<message TAGS:
to="you@yourAddress.com" • User defined.
• Case sensitive.
from="me@myAddress.com" • Must always have closing tags.

subject="XML Is Really Cool">• Must be properly nested.

<text>
How many ways is XML cool? Let me count

the ways...
</text> ATTRIBUTES:
Attribute name is followed by an equal sign and the attribute
</message> value, and multiple attributes are separated by spaces.
Unlike HTML, however, in XML commas between attributes
are not ignored; if present, they generate an error.
Attribute values must always be quoted.
©2004 Accenture All Rights Reserved. 6
© Accenture 2005 All Rights Reserved
WHAT IS XML?
Tags
CASE SENSITIVE:
INVALID
<Message> This is incorrect </message>
<message> This is correct </message>

MUST ALWAYS HAVE CLOSING TAGS:


<p> This is invalid
<p> This is valid </p>

MUST BE PROPERLY NESTED:


<b><i> This is not properly nested </b></i>
VALID
<b><i> This is properly nested </i></b>

©2004 Accenture All Rights Reserved. 7


© Accenture 2005 All Rights Reserved
INTRODUCTION TO XML
What is XML? (Empty Tags &
Comments)
<message
to="you@yourAddress.com"
from="me@myAddress.com"
subject="XML Is Really Cool">
<flag/> COMMENT

<!-- This is a comment -->


<text>
EMPTY TAGS:
How many ways isof tagXML
This kind does notcool? Let some
enclose any content, count
it’s known
as an empty tag. You create an empty tag by ending it
with /> instead of >.

the ways...
</text>
</message> ©2004 Accenture All Rights Reserved. 8
© Accenture 2005 All Rights Reserved
INTRODUCTION TO XML
What is XML? (The XML Prolog)
<?xml version="1.0"?>

The declaration may also contain additional information:

<?xml version="1.0" encoding="ISO-8859-1"


standalone="yes"?>
The XML declaration is essentially the same as the HTML header, <html>, except that it uses <?..?> and it may
contain the following attributes:

• version: Identifies the version of the XML markup language used in the data. This attribute is not optional.
• encoding: Identifies the character set used to encode the data. ISO-8859-1 is Latin-1, the Western
European and English language character set. (The default is 8-bit Unicode: UTF-8.)
• standalone: Tells whether or not this document references an external entity or an external data type
specification. If there are no external references, then “yes” is appropriate.

Note: The declaration is actually optional, but it’s a good idea to include it whenever you create an XML file. The
declaration should have the version number, at a minimum, and ideally the encoding as well. That standard
simplifies things if the XML standard is extended in the future and if the data ever needs to be localized for
different geographical regions. ©2004 Accenture All Rights Reserved. 9
© Accenture 2005 All Rights Reserved
INTRODUCTION TO XML
Why is XML Important?

 Plain Text
 Data Identification
 Stylability
 Inline Reusability
 Linkability
 Easily Processed
 Hierarchical
©2004 Accenture All Rights Reserved. 10
© Accenture 2005 All Rights Reserved
INTRODUCTION TO XML
How Can You Use XML?
 Traditional Data Processing
– Use where XML encodes the data for a program to
process.
 Document-Driven Programming
– Use where XML documents are containers that build
interfaces and applications from existing components.
 Binding
– Use where the DTD or schema that defines an XML data
structure is used to automatically generate a significant
portion of the application that will eventually process that
data. ©2004 Accenture All Rights Reserved. 11
© Accenture 2005 All Rights Reserved
GENERATING XML DATA
Writing a Simple XML File
CREATING THE FILE:
Using a standard text editor, create
a file called slideSample.xml.
slideSample.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->

WRITING THE DECLARATION:


Next, write the declaration, which
ADDING A COMMENT:
identifies the file as an XML document. Comments are ignored by XML
parsers. A program will never see them
unless you activate special settings in
the parser.

©2004 Accenture All Rights Reserved. 12


© Accenture 2005 All Rights Reserved
GENERATING XML DATA
Defining the Root Element
slideSample01.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->
<slideshow>

</slideshow>
After the declaration, every XML file defines exactly one element, known
as the root element. All other elements in the file are contained within
this element.

Note: XML element names are case-sensitive. The end tag must match
the start tag exactly.

©2004 Accenture All Rights Reserved. 13


© Accenture 2005 All Rights Reserved
ROOT ELEMENT
Adding Attributes to an Element
slideSample01.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->
<slideshow
title="Sample Slide Show"
date="Date of publication"
author="Yours Truly"
>
</slideshow>
A slide presentation has a number of associated data items, none of
which requires any structure. So it is natural to define these data items
as attributes of the slideshow element.

Note: Colons should be used with care or avoided, because they are
used when defining the namespace for an XML document.

©2004 Accenture All Rights Reserved. 14


© Accenture 2005 All Rights Reserved
ROOT ELEMENT
Adding Nested Elements
slideSample01.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->
<slideshow … >
<!-- TITLE SLIDE -->
<slide type="all">
<title>Wake up to
WonderWidgets!</title>
</slide>
</slideshow>XML allows for hierarchically structured data, which means that an
element can contain other elements.

©2004 Accenture All Rights Reserved. 15


© Accenture 2005 All Rights Reserved
ROOT ELEMENT
Adding HTML-Style Text
slideSample01.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->

<!-- TITLE SLIDE -->
<slide type="all">
<title>Wake up to WonderWidgets!</title>
</slide>
<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Why <em>WonderWidgets</em> are great</item>
<item>Who <em>buys</em> WonderWidgets</item>
</slide>
</slideshow>
Because XML lets you define any tags you want, it makes sense to
define a set of tags that look like HTML. In fact, the XHTML standard
does exactlyAccenture
©2004 that. All Rights Reserved. 16
© Accenture 2005 All Rights Reserved
ROOT ELEMENT
Adding an Empty Element
slideSample01.xml
<?xml version='1.0' encoding='utf-8'?>
<!-- A SAMPLE set of slides -->

<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Why <em>WonderWidgets</em> are great</item>
<item/>
<item>Who <em>buys</em> WonderWidgets</item>
</slide>
</slideshow>

Any element can be an empty element. All it takes is ending the tag with
/> instead of >.

©2004 Accenture All Rights Reserved. 17


© Accenture 2005 All Rights Reserved
ROOT ELEMENT
The Finished Product
<?xml version='1.0' encoding='utf-8'?>
slideSample01.xml
<!-- A SAMPLE set of slides -->
<slideshow
title="Sample Slide Show"
date="Date of publication"
author="Yours Truly"
>
<!-- TITLE SLIDE -->
<slide type="all">
<title>Wake up to WonderWidgets!</title>
</slide>
<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Why <em>WonderWidgets</em> are great</item>
<item/>
<item>Who <em>buys</em> WonderWidgets</item>
©2004 Accenture All Rights Reserved. 18
</slide>
© Accenture 2005 All Rights Reserved
What is DTD?
DTD (Document Type Definition)

 Defines the legal building blocks of an XML


document. It defines the document structure with
a list of legal elements.

 Can be declared inline in your XML document,


or as an external reference.

©2004 Accenture All Rights Reserved. 19


© Accenture 2005 All Rights Reserved
DTD
The building blocks of XML documents
Seen from a DTD point of view, all XML
documents (and HTML
documents) are made up by the following simple
building blocks:
 Elements
 Tags
 Attributes
 Entities
©2004 Accenture All Rights Reserved. 20
© Accenture 2005 PCDATA
 All Rights Reserved
DTD
 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
©2004 Accenture HTML elements are "hr",21
All Rights Reserved.
"br" and "img".
© Accenture 2005 All Rights Reserved
DTD
 Tags

– Tags are used to markup elements.


– A starting tag like <element_name> marks up the
beginning of an element, and an ending tag like
</element_name> marks up the end of an element.
Examples:
message element marked up with message
tags:
©2004 Accenture All Rights Reserved. 22
<message>some message in
© Accenture 2005 All Rights Reserved
DTD
 Attributes

– Attributes provide extra information about


elements.
– Attributes are always placed inside the starting tag
of an element. Attributes always come in
name/value pairs. The following "img" element
has additional information about a source file:
<img src="computer.gif" />
©2004 Accenture All Rights Reserved. 23
© Accenture 2005 All Rights Reserved
DTD
 Entities

– Entities are variables used to define common text.


Entity references are references to entities.
– Most of you know the HTML entity reference:
"&nbsp;". 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.

©2004 Accenture All Rights Reserved. 24


© Accenture 2005 All Rights Reserved
DTD
 PCDATA

– PCDATA means parsed character data.


– PCDATA is text that will be parsed by a parser.
Tags inside the text will be treated as markup and
entities will be expanded.

©2004 Accenture All Rights Reserved. 25


© Accenture 2005 All Rights Reserved
DTD
 CDATA

– CDATA also 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.

©2004 Accenture All Rights Reserved. 26


© Accenture 2005 All Rights Reserved
DTD
Internal DOCTYPE declaration
<!DOCTYPE root-element [element-
declarations]>
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>

©2004 Accenture All Rights Reserved. 27


© Accenture 2005 All Rights Reserved
DTD
External DOCTYPE declaration
<!DOCTYPE root-element SYSTEM “filename”>
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

And this is copy of the file “note.dtd” containing the DTD:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

©2004 Accenture All Rights Reserved. 28


© Accenture 2005 All Rights Reserved
Resources
Helpful Websites:

 http://java.sun.com
 http://www.xmlfiles.com/xml/

 http://www.w3schools.com/xml/default.asp

 http://www.w3schools.com/dtd/default.asp

 http://www.devarticles.com/c/a/XML/XML-An-Introduction/

 http://www.devarticles.com/c/a/XML/XML-Basics-Part-

One/1/
 http://www.xml.com/pub/a/98/10/guide0.html?page=2#AEN5

8 ©2004 Accenture All Rights Reserved. 29


 http://www.theukwebdesigncompany.com/articles/xml-
© Accenture 2005 All Rights Reserved
Questions:

©2004 Accenture All Rights Reserved. 30


© Accenture 2005 All Rights Reserved

S-ar putea să vă placă și