180 likes | 302 Vues
This comprehensive guide offers an in-depth introduction to Extensible Markup Language (XML), covering its key components, structure, and the use of Document Type Definitions (DTDs). You'll learn how XML is utilized in various applications, particularly focusing on its integration with Android development, including manifest files, activity registration, and managing permissions. The guide also explores the significance of well-formed XML documents, proper tag usage, and the role of comments. Discover best practices for using attributes and children in XML data representation, and how to validate XML documents.
E N D
Introduction to XML XML – Extensible Markup Language
Outline • XML Overview • XML Components • DTD – Document type definition
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
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
XML Overview • XML represents the contents of data – not presentation • To incorporate presentation, style sheets can be used
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)
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>
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>
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>
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
XML Components • Comments • Same as in HTML <!-- This is a comment --> • Comments CAN NOT appear: • Inside a tag • Inside another comment
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
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
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
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)>
DTD – Specifications example • Example <!ELEMENT Student (Identification, GradYear)> <!ELEMENT Identification (Name, Town?, Job*)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Town (#PCDATA)> <!ELEMENT Job (#PCDATA)>
Full example • On public drive • Example • Car.xml with embedded DTD • Car.xml with separate DTD file