What Is XML: Self-Describing Data Is The Data That Describes Both Its Content and Structure. Why XML
What Is XML: Self-Describing Data Is The Data That Describes Both Its Content and Structure. Why XML
Why xml
The main thing which makes XML truly powerful is its international
acceptance. Many corporation use XML interfaces for databases,
programming, office application mobile phones and more. It is due to its
platform independent feature.
Features and Advantages of XML
If you need to display dynamic data in your HTML document, it will take
a lot of work to edit the HTML each time the data changes.
With XML, data can be stored in separate XML files. This way you can
focus on using HTML/CSS for display and layout, and be sure that
changes in the underlying data will not require any changes to the
HTML.
With a few lines of JavaScript code, you can read an external XML file
and update the data content of your web page.
2) XML simplifies data sharing
XML data is stored in plain text format. This provides a software- and
hardware-independent way of storing data.
This makes it much easier to create data that can be shared by different
applications.
3) XML simplifies data transport
Exchanging data as XML greatly reduces this complexity, since the data
can be read by different incompatible applications.
4) XML simplifies Platform change
Different applications can access your data, not only in HTML pages, but
also from XML data sources.
With XML, your data can be available to all kinds of "reading machines"
(Handheld computers, voice machines, news feeds, etc), and make it
more available for blind people, or people with other disabilities.
6) XML can be used to create new internet languages
Components
Processing Instruction
Tags
Elements
Attributes
Entities
Comments
Contents
Processing Instruction
XML document begins with PI
Xml declaration
XML documents must contain one root element that is the parent of all
other elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
<student roll=”01”>
<name>John</name>
<grade>A+</grade>
</student>
The first line is the XML declaration. It defines the XML version (1.0) and
the encoding used (ISO-8859-1 = Latin-1/West European character set).
The next line describes the root element of the document (like saying:
"this document is a note"):
The terms parent, child, and sibling are used to describe the
relationships between elements. Parent elements have children.
Children on the same level are called siblings (brothers or sisters).
ll XML Elements Must Have a Closing Tag
In XML, it is illegal to omit the closing tag.
All elements must have a closing tag:
<p>This is a paragraph.</p>
<br />
"Opening and closing tags" are often referred to as "Start and end tags".
Use whatever you prefer. It is exactly the same thing.
XML Elements Must be Properly Nested
In the example above, "Properly nested" simply means that since the <i>
element is opened inside the <b> element, it must be closed inside the
<b> element.
XML Attribute Values Must Always be Quoted
XML elements can have attributes in name/value pairs just like in HTML.
If you place a character like "<" inside an XML element, it will generate
an error because the parser interprets it as the start of a new element.
To avoid this error, replace the "<" character with an entity reference:
<message>salary < 1000</message>
Only < and & are strictly illegal in XML, but it is a good habit to replace >
with > as well.
Comments in XML
File: books.xml
<bookstore>
<book category="COOKING">
<title lang="Eng">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="Eng">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="Eng">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Output
<bookstore>
<book category="COOKING">
<title lang="Eng">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="Eng">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="Eng">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Output
-<note>
<email>
<to>Vimal</to>
<from>Sonoo</from>
<heading>Hello</heading>
<body>Hello brother, how are you!</body>
</email>
<email>
<to>Peter</to>
<from>Jack</from>
<heading>Birth day wish</heading>
<body>Happy birth day Tom!</body>
</email>
<email>
<to>James</to>
<from>Jaclin</from>
<heading>Morning walk</heading>
<body>Please start morning walk to stay fit!</body>
</email>
<email>
<to>Kartik</to>
<from>Kumar</from>
<heading>Health Tips</heading>
<body>Smoking is injurious to health!</body>
</email>
</note>
XML Attributes
XML elements can have attributes. By the use of attributes we can add
the information about the element.
Data can be stored in attributes or in child elements. But there are some
limitations in using attributes, over child elements.
Why should we avoid XML attributes
Attributes cannot contain multiple values but child elements can have
multiple values.
Attributes cannot contain tree structure but child element can.
Attributes are not easily expandable. If you want to change in
attribute's values in future, it may be complicated.
Attributes cannot describe structure but child elements can.
Attributes are more difficult to be manipulated by program code.
2nd way:
<book>
<publisher> Tata McGraw Hill </publisher>
</book>
XML Comments
XML comments are just like HTML comments. We know that the
comments are used to make codes more understandable other
developers.
A tree structure contains root element (as parent), child element and so
on. It is very easy to traverse all succeeding branches and sub-branches
and leaf nodes starting from the root.
<?xml version="1.0"?>
<college>
<student>
<firstname>Tamanna</firstname>
<lastname>Bhatia</lastname>
<contact>09990449935</contact>
<email>[email protected]</email>
<address>
<city>Ghaziabad</city>
<state>Uttar Pradesh</state>
<pin>201007</pin>
</address>
</student>
</college>
Output
<college>
<student>
<firstname>Tamanna</firstname>
<lastname>Bhatia</lastname>
<contact>09990449935</contact>
<email>[email protected]</email>
<address>
<city>Ghaziabad</city>
<state>Uttar Pradesh</state>
<pin>201007</pin>
</address>
</student>
</college>