introduction to xml xml – extensible markup language

18
Introduction to XML XML – Extensible Markup Language

Upload: hillary-miller

Post on 21-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to XML XML – Extensible Markup Language

Introduction to XML

XML – Extensible Markup Language

Page 2: Introduction to XML XML – Extensible Markup Language

Outline

• XML Overview• XML Components• DTD – Document type definition

Page 3: Introduction to XML XML – Extensible Markup Language

XML Overview

• Resources– WWW consortium (W3C) Home page on XML• http://www.w3.org/XML

– XML 1.0 Spec• http://www.w3.org/TR/REC-xml/

– O’Reilly publishing XML resources• http://www.xml.com

Page 4: Introduction to XML XML – Extensible Markup Language

XML Overview

• What is the connection between XML and Android?– Manifest File

• Register activities• Required permissions• Other aspects of the application

– Developing Activities• Declarative

– XML – Google’s preferred method» Code is more concise» Less likely to change

• Visual Editor (generates XML) – editor still unwieldy• Procedural – within Java Code

Page 5: Introduction to XML XML – Extensible Markup Language

XML Overview

• XML represents the contents of data – not presentation

• To incorporate presentation, style sheets can be used

Page 6: Introduction to XML XML – Extensible Markup Language

XML Components

• XML elements and tags<Car>

Chevrolet Corvette

</Car>– In this example:• One element: Car• Car element starts and ends with tags

• XML is case–sensitive (unlike HTML)

Page 7: Introduction to XML XML – Extensible Markup Language

XML Components

• Parents, children, and siblings<Car>

<Identification><Make>Chevrolet</Make><Model>Corvette</Model><VIN>123</VIN>

</Identification><Engine>

<Displacement>6.2</Displacement><Cylinders>8</Cylinders>

</Engine>

</Car>

Page 8: Introduction to XML XML – Extensible Markup Language

XML Components

• Root element– Only element in document with no parent– Each document can have only 1 root element– Invalid document:

<car>…</car><boat>…</boat>

Page 9: Introduction to XML XML – Extensible Markup Language

XML Components

• Attributes– Name-value pair(s) assigned to tag– Identification element has 2 attributes and 1 child

<Car><Identification Make=“Chevrolet” Model=“Corvette”>

<VIN>123</VIN>

</Identification>

</Car>

Page 10: Introduction to XML XML – Extensible Markup Language

XML Components

• When to use attributes and when to use children?– Attributes are for metadata of element– Children are for information about element

• Subject of heated debate– No clear way to tell what is metadata and what is

information• Android – both used in specific instances

Page 11: Introduction to XML XML – Extensible Markup Language

XML Components

• Comments– Same as in HTML

<!-- This is a comment -->

– Comments CAN NOT appear:• Inside a tag• Inside another comment

Page 12: Introduction to XML XML – Extensible Markup Language

XML Components

• XML Declaration (the following is used in Android):

<?XML version=“1.0” encoding=“UTF-8”?>

• Version is the version of XML being used– 1.0 is current version

• Encoding is the type of text– UTF-8 is default

• Variable width: 1 – 4 bytes• Superset of ASCII

– Others• ASCII, UTF-16, others

Page 13: Introduction to XML XML – Extensible Markup Language

XML Components

• Checking for well-formedness– Start tags must have matching end tags– Elements may nest, but not overlap– There must be exactly one root element– Attribute values must be quoted– An element cannot have two same-named

attributes– Comments cannot be inside tags

Page 14: Introduction to XML XML – Extensible Markup Language

DTD – Document type definitions

• Written in formal syntax• Allow for describing:– What elements may appear– What elements’ contents and attributes are

• Usually stored in separate file– .dtd extension is typical– Information can be contained in associated .xml

file

Page 15: Introduction to XML XML – Extensible Markup Language

DTD – Document type definitions

• Example<!ELEMENT Car (Identification, Engine)><!ELEMENT Identification (Make, Model, VIN)><!ELEMENT Make (#PCDATA)><!ELEMENT Model (#PCDATA)><!ELEMENT VIN (#PCDATA)><!ELEMENT Engine (Displacement, Cylinders)><!ELEMENT Displacement (#PCDATA)><!ELEMENT Cylinders (#PCDATA)>

Page 16: Introduction to XML XML – Extensible Markup Language

DTD – Element specifications

Symbol Meaning

no symbol element must appear exactly once

? optional but if used, must appear exactly once

* element appears zero or more times

+ element must appear at least once

| choice of elements – but only one can be used

, sequence of elements that can be used (each element used according to its symbol)

Page 17: Introduction to XML XML – Extensible Markup Language

DTD – Specifications example

• Example<!ELEMENT Student (Identification, GradYear)><!ELEMENT Identification (Name, Town?, Job*)><!ELEMENT Name (#PCDATA)><!ELEMENT Town (#PCDATA)><!ELEMENT Job (#PCDATA)>

Page 18: Introduction to XML XML – Extensible Markup Language

Full example

• On public drive– Example• Car.xml with embedded DTD• Car.xml with separate DTD file