Cse2045Y Web Application Development: XML (Extensible Markup Language)
Cse2045Y Web Application Development: XML (Extensible Markup Language)
Lecture 3
XML (eXtensible Markup Language)
Agenda
• What is XML?
• HTML v/s XML
• Reasons to use XML
• How does XML work?
• XML Tree Structure
– XML Elements
– XML Attributes
• XML Rules
• XML Errors
2
What is XML?
• XML stands for eXtensible Markup Language.
4
HTML v/s XML (2)
3. Structured Data
– Specify the relations between elements
– E.g. Name must consist of First, Middle and Last
name.
6
XML Example 1
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML Example 2
<?xml version="1.0" encoding="UTF-8"?>
<breakfast>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast> 8
How Can XML be used?
• XML separates Data from Presentation
– XML does not carry any information about how to be
displayed.
– The same XML data can be used in many different
presentation scenarios.
10
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
11
12
XML Errors
• If the XML is not well-formed, the error will be
hignlighted.
13
14
XML Tree Elements (2)
• The terms parent, child, and sibling are used to
describe the relationships between elements.
15
16
Self-Describing Syntax (2)
• The <book> elements have 4 child elements:
<title>, <author>, <year>, <price>.
17
18
XML Entity References (2)
• There are 5 pre-defined entity references in
XML:
19
Activity 1
• Given the following XML Tree diagram, write
the XML.
https://www.tutorialspoint.com/xml/images/tree_structure.jpg 20
XML - is that it?
• Well that was fairly easy, most probably easier
than HTML since I do not have to remember tags
like <h1>, <br/>, <div> and so on
• Question
– How do we validate whether the XML is correct?
https://www.tutorialspoint.com/xml/images/syntaxrules.png
22
XML Syntax Rules (2)
• XML Documents Must Have a Root Element.
• All XML Elements Must Have a Closing Tag.
• XML Tags are Case Sensitive.
• XML Elements Must be Properly Nested.
• XML Attribute Values Must be Quoted.
23
25
26
Activity 2
• Which of these XML elements are invalid? State
why?
<123>
<xml-tag>
<my tag>
27
XML Attributes
• Attributes are name/value pairs associated with
an Element.
• Value must be in quotes (either single or double
quotes).
– We can use single on some attributes and double on
others, but you can't mix them in a single attribute.
• No element can contain two attributes with the
same name.
• XML elements can have more then one
attributes.
28
XML Attributes Examples
<book category="cooking">
<male gender="male">
<male gender='male'>
29
Activity 3
• Which of these XML attributes are invalid? State why?
<input checked>
<input checked='true">
30
XML Comments
• Comments start with the string <!--
• and end with the string -->
• Example:
31
<name>
<first>John</first>
<middle>Fitzgerald Johansen</middle>
<last>Doe</last>
</name>
33
XML Errors
• An error is simply a violation of the rules in the
recommendation, where the results are
undefined; the XML processor is allowed to
recover from the error and continue processing.
34
XML Validation (Next Week)
• The minimal requirement for an XML document is
well-formedness !
References
• https://www.w3schools.com/xml
• https://www.tutorialspoint.com/xml/xml_tree
_structure.htm
• http://www.w3webtutorial.com/xml/xml-
attribute.php
36