Unit 3
Unit 3
XML-:
XML or eXtensible Markup Language, is a way to structure and organize information in a
text format that is easy for computers to read. It uses tags, similar to those in HTML, to
define different types of data inside a document. XML allows data to be stored and shared in
a consistent and structured manner, which makes it useful for tasks like storing configuration
settings, exchanging data between different systems, and managing information in databases.
XML stands for extensible Markup Language.
XML was designed to store and transport data.
XML is a markup language much like HTML
Xml was released in late 90’s. it was created to provide an easy to use and store self
describing data.
XML became a W3C Recommendation on February 10, 1998.
XML is not a replacement for HTML.
XML is designed to be self-descriptive.
XML is designed to carry data, not to display data.
XML tags are not predefined. You must define your own tags.
XML is platform independent and language independent.
<message>
<text>Hello, world!</text>
</message>
History of XML
XML started way back in 1996 and was first published in
1998. World Wide Web Consortium (W3C) is the developer of XML, and it became a W3C
recommendation in 1998.
There are two versions of XML.
1. XML 1.0
2. XML 1.1
XML 1.1 is the latest version. Yet, XML 1.0 is the most used version.
XML Features
Here are some important features of XML:
It is extensible and human-readable.
It is platform and language independent.
It preserves white space.
Overall simplicity.
Self-descriptive nature.
It separates data from HTML.
XML tags are not predefined. You need to define your customized tags.
XML was designed to carry data, not to display that data.
Mark-up code of XML is easy to understand for a human.
Well-structured format is easy to read and write from programs.
XML is an extensible markup language like HTML.
XML Syntax
The below code segment shows the basic XML syntax.
<?xml version = "1.0" encoding = "UTF-8" ?>
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
Example 1
<?xml version="1.0" encoding="UTF-8"?>
<shop>
<fruit>
<name>Apple</name>
<price>300</price>
<calories>650</calories>
</fruit>
<fruit>
<name>Grapes</name>
<price>200</price>
<calories>350</calories>
</fruit>
<fruit>
<name>Mango</name>
<price>800</price>
<calories>500</calories>
</fruit>
</shop>
Example 2-
<?xml version="1.0" encoding="UTF-8"?>
<college>
<student>
<rollno>101</rollno>
<name>Sakshi</name>
<address>pune</address>
</student>
<student>
<rollno>102</rollno>
<name>Aarti</name>
<address>pune</address>
</student>
<student>
<rollno>103</rollno>
<name>Akanksha</name>
<address>pune</address>
</student>
</college>
Example 3
<?xml version="1.0" encoding="UTF-8"?>
<India>
<state>Maharashtra </state>
<capital>Mumbai</capital>
<city>Pune</city>
<state>Gujrat</state>
<capital>Gandhinagar</capital>
<city>Ahmedabad</city>
<state>Rajasthan </state>
<capital> Jaipur </capital>
<city>Udaipur</city>
</India>
XML Parsers –:
In PHP, an XML parser is used to read and manipulate XML data. An XML parser is a
software library or package that provides interfaces for client applications to work with an
XML document. The XML Parser is designed to read the XML and create a way for programs
to use XML.
XML parser validates the document and check that the document is well formatted.
Let's understand the working of XML parser by the figure given below:
PHP provides several ways to parse XML, and the two main ways to handle XML are
using:
<?php
$xmlData='<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Ankita</to>
<from>Pune</from>
<heading>Php Project</heading>
</note>';
print_r($xml);
?>
OUTPUT
SimpleXMLElement Object ( [to] => Ankita [from] => Pune [heading] => Php
Project )
Example
note.xml (create xml file)
print_r($xml);
?>
OUTPUT
SimpleXMLElement Object ( [to] => Ankita [from] => Pune [heading] => Php Project )
---------------------------------------------------------------------------------------------------------------------------