+ All Categories
Home > Documents > M15 - XML

M15 - XML

Date post: 02-Jun-2018
Category:
Upload: shaharukh-nadaf
View: 217 times
Download: 0 times
Share this document with a friend

of 36

Transcript
  • 8/10/2019 M15 - XML

    1/36

    Module 15: XML

    Java Course

  • 8/10/2019 M15 - XML

    2/36

    2

    Module Objective

    At the end of this module, participants will be able to: Explain the concept and use of XML

    Describe the XML tree structure

    Identify and adhere to XML syntax rules

    Use XML elements and attributes

    Describe how XML documents are validated

    Use XML schemas for XML document validation

  • 8/10/2019 M15 - XML

    3/36

    3

    XML Overview

    XML is a markup language for documents containing structuredinformation

    XML is used to describe, store, and transport data

    XML is pure information wrapped in user-defined tags

    XML was created so that richly structured documents could be

    used over the internet

  • 8/10/2019 M15 - XML

    4/36

  • 8/10/2019 M15 - XML

    5/36

    5

    XML Tree Structure

    Elements in XML documents form a logical tree structure

    XML tree structure illustrates the hierarchy and locality of the

    elements in a XML document

    XML tree structure can help in showing which elements are the

    descendants and ancestors of each element

    ** Refer to the Classroom.xml sample code

  • 8/10/2019 M15 - XML

    6/36

    6

    XML Tree Structure

    Sample code:

    Victoria

    BrookeFemale

    30

    MichaelRogers

    Male

    18

  • 8/10/2019 M15 - XML

    7/367

    XML Tree Structure

    XML Tree of previous code:

    CLASSROOM

    TEACHER STUDENT

    FirstName

    LastName

    Gender AgeFirstName

    LastName

    Gender Age

  • 8/10/2019 M15 - XML

    8/368

    XML Syntax Rules

    All XML documents should begin with an XML declaration. The XMLdeclaration is a processing instruction that identifies the document asbeing XML.

    Example:

    There are no predefined tags in XML, users have to make their own

    tags

    XML documents must have exactly one root element, also known asthe document element.

    Basic syntax for XML elements:

    Syntax : element value

    Examples: Jason

    My Favorite Book

  • 8/10/2019 M15 - XML

    9/369

    XML Syntax Rules

    All XML elements must have a corresponding closing tag

    Invalid: some value.

    Valid : some value placed here

    XML tags are case sensitive

    Invalid: 7 Tonner Rice

    Valid : 7 Tonner Rice

  • 8/10/2019 M15 - XML

    10/3610

    XML Syntax Rules

    XML elements must be properly nested

    Invalid:

    Some value here

    Some value here

    Valid :

    Some value here

    Another value here

  • 8/10/2019 M15 - XML

    11/36

    11

    XML Syntax Rules

    XML attribute values must be placed within quotes

    Invalid: .

    Valid : .

    Make use of entity references for special characters

    < < Less than symbol

    > > Greater than symbol

    & & Ampersand

    ' Apostrophe

    " Quotation mark

  • 8/10/2019 M15 - XML

    12/36

  • 8/10/2019 M15 - XML

    13/36

    13

    XML Elements

    Elements are used to classify data in an XML document to makethe data understandable.

    Elements can have any name desired and are usually

    descriptive of the data they hold.

    Elements can contain other elements usually to include more

    details.

  • 8/10/2019 M15 - XML

    14/36

    14

    XML Elements

    Elements can contain attributes which also allow additionalinformation.

    Elements are defined by its opening and closing tags.

    This sentence is found inside the element -myElement-

    Opening tag Closing tagInformation stored in the element

  • 8/10/2019 M15 - XML

    15/36

    15

    XML Attributes

    XML Attributes provide additional information to the element towhich it belongs.

    Attributes are information that is often not part of data but is

    used in manipulating the data the element holds.

    Attributes are commonly used for identification purposes, in

    cases as such there is more than one element of the same type.

  • 8/10/2019 M15 - XML

    16/36

    16

    XML Attributes

    XML Attribute Sample:

    Anna

    Sanders

    Female

    John

    dela Cruz

    Male

    ** Refer to the classlist.xml sample code

  • 8/10/2019 M15 - XML

    17/36

    17

    Validating XML Documents

    Validating of XML documents is done through a DTD (DocumentType Definition) or an XSD (XML Schema Definition)

    Different types of XML documents in terms of validity

    Broken XML documents

    WellFormed XML Documents

    Valid XML Documents

  • 8/10/2019 M15 - XML

    18/36

    18

    Validating XML Documents

    Broken XML documents refer to XML Documents where syntaxrules are violated

    Well-formed XML documents refer to XML documents that fully

    comply to the syntax rules

    Valid XML documents refer to XML documents that are well-

    formed and comply to a DTD/XSD

  • 8/10/2019 M15 - XML

    19/36

    19

    Validating XML Documents

    A DTD defines the structure of an XML document with the list oflegal elements and attributes.

    XML Schema Definition (XSD) is the XML-based alternative of

    DTDs, having the same purpose of DTDs but more powerful and

    extensible.

    DTD is the older standard. It is most probable that XSD will

    replace DTD for validating XML Documents.

    ** Refer to the person_DTD.xml sample code

  • 8/10/2019 M15 - XML

    20/36

    20

    Validating XML Documents

    DTD vs XML Schema (XSD)

    XSD is extensible to accept future additions

    XSD is more powerful than its predecessor

    XSD makes use of XML syntax

    XSD supports data types

    XSD supports namespaces

    ** Refer to the person_XSD.xml and person.xsd sample code

  • 8/10/2019 M15 - XML

    21/36

    21

    Validating XML Documents

    XSD is written similarly to XML as XSD makes use of XMLsyntax, hence most of the rules of XML apply to XSD

    XML Schema has data types and namespaces, unlike DTDs

    XML Schema data types include

    String

    Date

    Numeric

    Many others

  • 8/10/2019 M15 - XML

    22/36

    22

    Validating XML Documents

    The element is the root element for XSD

    Syntax :

    The element can have attributes, including the

    default namespace to be used

  • 8/10/2019 M15 - XML

    23/36

    23

    Validating XML Documents

    Attributes of elements are defined within the elements where the

    attributes belong

    Syntax :

    Example :

    ** Refer to the Samples.xml and XSD_Samples.xsd sample code

  • 8/10/2019 M15 - XML

    24/36

    24

    Validating XML Documents

    Elements in schemas define the structure and properties ofelements in XML documents

    Elements in schemas are divided into two types

    Simple Elements

    Complex Elements

  • 8/10/2019 M15 - XML

    25/36

    25

    Validating XML Documents

    Simple elements refer to elements containing only text andcannot contain other elements or attributes

    In XML : Michael Angelo

    Syntax :

    Example :

    Simple elements can have default or fixed values

    Syntax :

    Examples :

  • 8/10/2019 M15 - XML

    26/36

    26

    Validating XML Documents

    Complex elements refer to elements that can have attributesand can also be one of the following:

    Elements that contain only text (with attributes)

    Elements that are empty

    Elements containing other elements

    Elements that contain both text and other elements

  • 8/10/2019 M15 - XML

    27/36

    27

    Validating XML Documents

    Elements that contain only text but have attributes areconsidered complex elements

    XML : Spike

    XSD :

  • 8/10/2019 M15 - XML

    28/36

  • 8/10/2019 M15 - XML

    29/36

    29

    Validating XML Documents

    Elements that serve only to contain other elements areconsidered complex elements

    XML :

    red

    4

    XSD :

  • 8/10/2019 M15 - XML

    30/36

    30

    Validating XML Documents

    Elements that contain both text and other elements areconsidered complex elements

    XML : Dear Mrs.

    Erika Daniels. Your child,

    Michael, has done something at school.

    Please come to the prinicipals office anytime tomorrow,

    2008-08-21.

    XSD :

  • 8/10/2019 M15 - XML

    31/36

    31

    Validating XML Documents

    Indicators control how elements can be used Order Indicators

    All

    Choice

    Sequence

    Occurrence Indicators maxOccurs

    minOccurs

    Group Indicators

    Group name

    attributeGroup name

  • 8/10/2019 M15 - XML

    32/36

    32

    Validating XML Documents

    The element allows other elements not specified in theschema, which makes it extensible

    Similarly, the element allows other attributes not

    specified in the schema within the designated element

  • 8/10/2019 M15 - XML

    33/36

  • 8/10/2019 M15 - XML

    34/36

    34

    Validating XML Documents

    Restrictions set the acceptable values for elements andattributes in XML documents

    Some restrictions on values that can be applied are

    Set of values

    Series of values

    Whitespace character

    Length Restrictions

  • 8/10/2019 M15 - XML

    35/36

    35

    References

    W3SchoolsXML Tutorial

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

    Java SE 6 Documentation, Tutorials, Training, Demos, and

    Samples http://java.sun.com/javase/6/docs/index.html

    Tizag - XML Tutorial

    http://www.tizag.com/xmlTutorial/index.php

    http://www.w3schools.com/xml/default.asphttp://java.sun.com/javase/6/docs/index.htmlhttp://www.tizag.com/xmlTutorial/index.phphttp://www.tizag.com/xmlTutorial/index.phphttp://java.sun.com/javase/6/docs/index.htmlhttp://www.w3schools.com/xml/default.asp
  • 8/10/2019 M15 - XML

    36/36

    Questions and Comments


Recommended