Sie sind auf Seite 1von 30

XML

EXtensibleMarkupLanguage

By Sandeep
Sharma

XML stands forExtensibleMarkupLanguage and


is a text-based markup language derived from
Standard Generalized Markup Language
(SGML)

XML was designed to store and transport data.


XML was designed to be both human- and

machine-readable.
2

XML Example 1
<?xmlversion="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
<?xmlversion="1.0"encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Our famous Belgian Waffles with plenty of real maple
syrup</description>
<calories>650</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough
bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash
browns</description>
<calories>950</calories>
</food>
</breakfast_menu>

Output:

Introduction toXML
XML is a software- and
hardware-independent tool
for storing and transporting
data.
6

Why Study XML?


XML plays an important role in many IT systems.
For this reason, it is important for all software developers to have a
good understanding of XML.

Before you continue, you should also have a basic


understanding of:
HTML
JavaScript

What is XML?
XML stands for EXtensible Markup Language
XML is a markup language much like HTML
XML was designed to store and transport data
XML was designed to be self-descriptive
XML is a W3C Recommendation

XML Does Not DO Anything


Maybe it is a little hard to understand, but XML does not DO anything.
This note is a note to Tove, from Jani, stored as XML:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The note is quite self-descriptive. It has sender and receiver information. It also has a heading
and a message body. But still, this XML document does not DO anything. XML is just
information wrapped in tags. Someone must write a piece of software to send, receive, store,
or display it:

The Difference Between XML and


HTML

XML and HTML were designed with different goals:

XML was designed to carry data - with focus on what data is

HTML was designed to display data - with focus on how


data looks

XML tags are not predefined like HTML tags are

10

XML Does Not Use Predefined Tags


The XML language has no predefined 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.
HTML works with predefined tags like <p>, <h1>, <table>, etc.
With XML, the author must define both the tags and the document
structure.

11

XML is Extensible

Most XML applications will work as expected even if new


data is added (or removed). Imagine an application
designed to display the original version of note.xml (<to>
<from> <heading> <data>).

Then imagine a newer version of note.xml with added


<date> and <hour> elements, and a removed <heading>.

The way XML is constructed, older version of the application


can still work:

<note>
<date>2015-09-01</date>
<hour>08:30</hour>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>

12

XML Simplifies Things


It simplifies data sharing
It simplifies data transport
It simplifies platform changes
It simplifies data availability

13

Many computer systems contain data in incompatible formats. Exchanging


data between incompatible systems (or upgraded systems) is a timeconsuming task for web developers. Large amounts of data must be
converted, and incompatible data is often lost.

XML stores data in plain text format. This provides a software- and
hardware-independent way of storing, transporting, and sharing data.

XML also makes it easier to expand or upgrade to new operating systems,


new applications, or new browsers, without losing data.

With XML, data can be available to all kinds of "reading machines" like
people, computers, voice machines, news feeds, etc.
14

XML is a W3C
Recommendation
Became a W3C
ecommendation on
February 10, 1998.
15

How Can XML beUsed?


XML is used in many aspects of web
development.
XML is often used to separate data from
presentation.

16

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.
Because of this, with XML, there is a full separation between data and
presentation.
17

XML is Often a Complement to


HTML
In many HTML applications,
XML is used to store or transport data,
while HTML is used to format and display the
same data.
18

XML Separates Data from


HTML
When displaying data in HTML, you should not have to edit
the HTML file when the data changes.
With XML, the data can be stored in separate XML files.
With a few lines of JavaScript code, you can read an XML file
and update the data content of any HTML page.
19

20

Books.xml
<?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">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web"cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

21

Transaction Data
Thousands of XML formats exists, in many different
industries, to describe day-to-day data
transactions:

22

Example: XML News


XMLNews is a specification for exchanging news

and other information.


Using a standard makes it easier for both news

producers and news consumers to produce, receive,


and archive any kind of news information across
different hardware, software, and programming
languages.
23

Example: XML News


<?xmlversion="1.0"encoding="UTF-8"?>
<nitf>
<head>
<title>Colombia Earthquake</title>
</head>
<body>
<headline>
<hl1>143 Dead in Colombia Earthquake</hl1>
</headline>
<byline>
<bytag>By Jared Kotler, Associated Press
Writer</bytag>
</byline>
<dateline>
<location>Bogota, Colombia</location>
<date>Monday January 25 1999 7:28
ET</date>
</dateline>
</body>
</nitf>

24

Example: XML Weather Service


<?xmlversion="1.0"encoding="UTF-8"?>
<current_observation>

An XML national weather


service from NOAA
(National Oceanic and
Atmospheric
Administration)

<credit>NOAA's National Weather Service</credit>


<credit_URL>http://weather.gov/</credit_URL>
<image>
<url>http://weather.gov/images/xml_logo.gif</url>
<title>NOAA's National Weather Service</title>
<link>http://weather.gov</link>
</image>
<location>New York/John F. Kennedy Intl Airport, NY</location>
<station_id>KJFK</station_id>
<latitude>40.66</latitude>
<longitude>-73.78</longitude>
<observation_time_rfc822>Mon, 11 Feb 2008 06:51:00 -0500
EST
</observation_time_rfc822>
<weather>A Few Clouds</weather>
<temp_f>11</temp_f>
<temp_c>-12</temp_c>
<relative_humidity>36</relative_humidity>
<wind_dir>West</wind_dir>
<wind_degrees>280</wind_degrees>
<wind_mph>18.4</wind_mph>
<wind_gust_mph>29</wind_gust_mph>
<pressure_mb>1023.6</pressure_mb>
<pressure_in>30.23</pressure_in>
<dewpoint_f>-11</dewpoint_f>
<dewpoint_c>-24</dewpoint_c>
<windchill_f>-7</windchill_f>
<windchill_c>-22</windchill_c>
<visibility_mi>10.00</visibility_mi>
<icon_url_base>http://weather.gov/weather/images/fcicons/</ico
n_url_base>
<icon_url_name>nfew.jpg</icon_url_name>
<disclaimer_url>http://weather.gov/disclaimer.html</disclaimer_
url>

25

XMLTree
XML documents form a tree structure
that starts at "the root" and branches to
"the leaves".

26

27

The image above represents books in this XML:


<?xmlversion="1.0"encoding="UTF8"?>
<bookstore>
<bookcategory="cooking">
<titlelang="en">Everyday
Italian</title>
<author>Giada De
Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<bookcategory="children">
<titlelang="en">Harry
Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<bookcategory="web">
<titlelang="en">Learning
XML</title>
<author>Erik T. Ray</author>

28

XML Tree Structure

XML documents are formed aselement trees.

An XML tree starts at aroot elementand branches from the root tochild
elements.

All elements can have sub elements (child elements):


<root>
<child>
<subchild>.....</subchild>
</child>
</root>

The terms parent, child, and sibling are used to describe the relationships between
elements.
Parent have children. Children have parents. Siblings are children on the same level
(brothers and sisters). All elements can have text content (Harry Potter) and
attributes (category="cooking").
29

Self-Describing Syntax

30

Das könnte Ihnen auch gefallen