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 – – –
Generating XML Data – –
What is XML? Why is XML important? How Can You Use XML? Writing a Simple XML File Defining the Root Element
DTD
© Accenture 2005 All Rights Reserved
©2004 Accenture All Rights Reserved.
2
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.
© Accenture 2005 All Rights Reserved
3
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.
© Accenture 2005 All Rights Reserved
4
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. HTML standard.
© Accenture 2005 All Rights Reserved
5
INTRODUCTION TO XML
What is XML? (Tags & Attributes) <message TAGS: • User defined. to="
[email protected]" • Case sensitive. from="
[email protected]" • Must always have closing tags. • Must be properly nested. subject="XML Is Really Cool">
How many ways is XML cool? Let me count the ways...
ATTRIBUTES: Attribute name is followed by an equal sign and the attribute 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. © Accenture 2005 All Rights Reserved
6
WHAT IS XML?
Tags CASE SENSITIVE: <Message> This is incorrect <message> This is correct
INVALID
MUST ALWAYS HAVE CLOSING TAGS:
This is invalid
This is valid
MUST BE PROPERLY NESTED:
This is not properly nested VALID
This is properly nested
©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
7
INTRODUCTION TO XML
What is XML? (Empty Tags & Comments)
<message to="
[email protected]" from="
[email protected]" subject="XML Is Really Cool"> COMMENT
EMPTY TAGS: How many ways isof tagXML Let some count This kind does notcool? enclose any content, it’s known as an empty tag. You create an empty tag by ending it with /> instead of >.
the ways... ©2004 Accenture
© Accenture 2005 All Rights Reserved
All Rights Reserved.
8
INTRODUCTION TO XML
What is XML? (The XML Prolog) The declaration may also contain additional information:
The XML declaration is essentially the same as the HTML header, , 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.
© Accenture 2005 All Rights Reserved
10
INTRODUCTION TO XML
How Can You Use XML?
Traditional Data Processing –
Document-Driven Programming –
Use where XML encodes the data for a program to process. 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
WRITING THE DECLARATION: Next, write the declaration, which identifies the file as an XML document.
ADDING A COMMENT: 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. © Accenture 2005 All Rights Reserved
12
GENERATING XML DATA
Defining the Root Element
slideSample01.xml
<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. © Accenture 2005 All Rights Reserved
13
ROOT ELEMENT
Adding Attributes to an Element
slideSample01.xml
<slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly" > 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. © Accenture 2005 All Rights Reserved
14
ROOT ELEMENT
Adding Nested Elements
slideSample01.xml
encoding='utf-8'?> <slideshow … > <slide type="all">
Wake up to WonderWidgets! XML allows for hierarchically structured data, which means that an element can contain other elements.
©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
15
ROOT ELEMENT
Adding HTML-Style Text
slideSample01.xml
… <slide type="all">
Wake up to WonderWidgets! <slide type="all">
Overview - Why <em>WonderWidgets are great
- Who <em>buys WonderWidgets
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 that. ©2004 All Rights Reserved.
© Accenture 2005 All Rights Reserved
16
ROOT ELEMENT
Adding an Empty Element
slideSample01.xml
… <slide type="all">
Overview - Why <em>WonderWidgets are great
- Who <em>buys WonderWidgets
Any element can be an empty element. All it takes is ending the tag with /> instead of >.
©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
17
ROOT ELEMENT
The Finished Product
slideSample01.xml
<slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly" > <slide type="all">
Wake up to WonderWidgets! <slide type="all">
Overview - Why <em>WonderWidgets are great
- Who <em>buys WonderWidgets
©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
18
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.
© Accenture 2005 All Rights Reserved
19
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 © Accenture 2005 PCDATA All Rights Reserved
©2004 Accenture All Rights Reserved.
20
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 elements are "hr",21 ©2004 Accenture HTML All Rights Reserved. © Accenture 2005 All Rights Reserved "br" and "img". –
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 marks up the end of an element.
Examples: message element marked up with message tags: ©2004 Accenture All Rights Reserved. <message>some message in
© Accenture 2005 All Rights Reserved
22
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:
©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
23
DTD
Entities –
–
Entities are variables used to define common text. Entity references are references to entities. Most of you know the HTML entity reference: " ". 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.
© Accenture 2005 All Rights Reserved
24
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. © Accenture 2005 All Rights Reserved
25
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. © Accenture 2005 All Rights Reserved
26
DTD Internal DOCTYPE declaration ]> <note>
Tove Jani Reminder Don't forget me this weekend
©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
27
DTD External DOCTYPE declaration <note>
Tove Jani Reminder Don't forget me this weekend!
And this is copy of the file “note.dtd” containing the DTD: ©2004 Accenture All Rights Reserved. © Accenture 2005 All Rights Reserved
28
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-PartOne/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. © Accenture 2005 All Rights Reserved
30