Chapter 4
Chapter 4
Development &
Management
XML
1
XML
• Stands for Extensible Markup Language
• XML was designed to carry, store and exchange
data
• It was not designed to display data
• Tags are added to the document to provide the
extra information
• HTML tells the browser how to display the
document
• XML tags give a reader some idea what some of
the data means (self descriptive).
• An XML document resides in its own file with an
‘.xml’ extension.
Difference between HTML and XML
• XML is not a replacement for HTML
• HTML tags have a fixed meaning and browsers
know what it is.
• XML tags are different for different applications,
and users know what they mean.
• HTML tags are used for display.
• XML tags are used to describe documents
Example 1
<note>
<to>John</to>
<from>Alex</from>
<heading>Reminder</heading>
<body>Don’t forget me this
weekend!</body>
</note>
With XML You Invent Your Own
Tags
• The tags in the example above (like <to> and
<from>) are not defined in any XML standard.
• These tags are "invented" by the author of the
XML document.
• That is because the XML language has no
predefined tags.
• The tags used in HTML are predefined.
• HTML documents can only use tags defined in
the HTML standard (like <p>, <h1>, etc.).
• XML allows the author to define his/her own tags
and his/her own document structure.
Comments
• <!-- This is a comment -->
• Comments in xml are useful for:
– Explaining the structure of an XML document
– Commenting out parts of the XML during
development and testing
• Comments are not elements and do not have an
end tag.
• Comments are not displayed by browsers, but
can be seen by anyone who looks at the source
code.
What is xml used for
• XML separates data from HTML
• XML documents are used to transfer
data from one place to another over
the internet
• XML subsets are designed to
separate particular applications
E.g. RSS(Rich Site Summary ) is used to send
breaking news bulletins from one site to another
• XML can make your data more useful
Contd’
• Every XML document has a tree
structure, so complex data can be
arranged & understood in a simple
manner.
• XML files are independent of an
operating system
XML Trees
• XML documents form a tree structure that
starts at "the root" and branches to "the
leaves“
• This element is "the parent" of all other
elements.
• An XML document has a single root node.
• <root>
<child>
<subchild>.....</subchild>
</child>
</root>
• In the above example 1
– <note> root element
The next 4 lines describe 4 child elements of the
root (to, from, heading, and body):
– <to>John</to> -child element to root and sibling to
element <from>
– <from>Alex</from>
• <heading>Reminder</heading>
– heading is element name and Reminder is element
value
• <body>Don’t forget me this
weekend!</body>
Note: Children on the same level are called
siblings (brothers or sisters)
XML Rules
• Tags are enclosed in angle brackets.
• Tags come in pairs with start-tags and end-
tags.
• Tags must be properly nested.
– <name><email>…</name></email> is not
allowed.
– <name><email>…</email><name> is
allowed.
• Tags that do not have end-tags must be
terminated by a ‘/’.
– <br /> is an html example.
• Tags are case sensitive.
– <address> is not the same as <Address>
• XML in any combination of cases is not
allowed as part of a tag. E.g. xml, Xml, …
• Tags may not contain ‘<‘ or ‘&’.
• Tags follow Java naming conventions
(agreement), except that a single colon and
other characters are allowed.
• They must begin with a letter and may not
contain white space.
• Documents must have a single root tag that
begins the document.
XML Elements
• An XML element is everything from (including)
the element's start tag to (including) the
element's end tag.
• An element can contain other elements, simple
text or a mixture of both.
• Elements can also have attributes.
<?xml version=“1.0”/>
<address>
<name>John Alex </name>
<email>[email protected]</email>
<phone>+251-999-327-335</phone>
<birthday>2000-03-22</birthday>
</address>
Element Naming
• Names can contains letters, numbers, and other
characters
• Names must not start with a number or
punctuation character
• Names must not start with the letters xml (or
XML, or Xml, etc.)
• Names cannot contain spaces
• Use under score or capitalization if the name is
more than one words
• Avoid using non-English letters
• The “:”. Is reserved character
XML Attributes
• Used to specify the values of the element.
– E.g: <file type=“gif”> computer.gif</file>
• Here type is the attribute with the value
gif.
• Attribute value must be enclosed within
double/single quotes
<file type=“gif”> computer.gif</file>
<file type=‘gif’> computer.gif</file>
•
XML Elements vs. Attributes
• <person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
• <person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
• In the first example sex is an attribute.
• In the last, sex is an element. Both examples
provide the same information.
• There are no rules about when to use attributes or
when to use elements.
Well-Formed Documents
• An XML document is said to be well-formed if it
follows all the rules.
• An XML parser is used to check that all the
rules have been obeyed.
• Recent browsers such as Internet Explorer 5
and Netscape 7 come with XML parsers.
• Parsers are also available for free download
over the Internet.
• One is Xerces, from the Apache open-source
project.
• Java 1.4 also supports an open-source parser
DTD(Data Type Definition)
• A DTD describes the tree structure of a
document and something about its data.
• There are two data types:
1. PCDATA.
• PCDATA is parsed character data.
2. CDATA
• CDATA is character data, not usually parsed.
A DTD determines how many times a node
may appear, and how child nodes are
ordered.
XML Schema
• Schemas are themselves XML documents.
• They were standardized after DTDs and provide
more information about the document.
• They have a number of data types including
string,
decimal, integer, Boolean, date, and time.
• They divide elements into simple and complex
types.
• They also determine the tree structure and how
many children a node may have.
NB. Refer on how DTD and Schema works
Quiz (10%)
1. Create an XML document which will list
your close relatives (For example father &
mother). XML elements that you have to
design includes
<firstName>
<lastName>
<age>
<occupation>
<phoneNumber>
THANK YOU!