0% found this document useful (0 votes)
10 views8 pages

Unit 3

The document provides an overview of XML (eXtensible Markup Language), detailing its structure, features, syntax, and history, including its development by the W3C and its recommendation status since 1998. It also explains XML document structure, the role of XML parsers in PHP, and introduces two main types of parsers: DOM and SAX, along with their advantages and disadvantages. Additionally, the document includes examples of XML syntax and how to read XML data using PHP's SimpleXML functions.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Unit 3

The document provides an overview of XML (eXtensible Markup Language), detailing its structure, features, syntax, and history, including its development by the W3C and its recommendation status since 1998. It also explains XML document structure, the role of XML parsers in PHP, and introduces two main types of parsers: DOM and SAX, along with their advantages and disadvantages. Additionally, the document includes examples of XML syntax and how to read XML data using PHP's SimpleXML functions.

Uploaded by

shraddhavinod1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT 3

PHP AND XML

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 Document Structure-:


An XML document is a basic unit of XML information composed of elements and other
markup in an orderly package. An XML document can contain a wide variety of data. For
example, database of numbers, numbers representing molecular structure or a mathematical
equation.
XML Document Example
A simple document is shown in the following example:
<?xml version="1.0"?>
<contact-info>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</contact-info>
The following image depicts the parts of XML document.

Document Prolog Section


Document Prolog comes at the top of the document, before the root element. This section
contains:
XML declaration Document type declaration
Document Elements Section
Document Elements are the building blocks of XML. These divide the document into a
hierarchy of sections, each serving a specific purpose. You can separate a document into
multiple sections so that they can be rendered differently, or used by a search engine. The
elements can be containers, with a combination of text and other elements.

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:

1. SimpleXML (which is very easy and intuitive to use).

i) Php simpleXML-: ( Read From String)


simpleXML_load_string() function is used to read xml data from a string.

<?php
$xmlData='<?xml version="1.0" encoding="UTF-8"?>

<note>
<to>Ankita</to>
<from>Pune</from>
<heading>Php Project</heading>
</note>';

$xml=simplexml_load_string($xmlData) or die ("Error: cannot create object");

print_r($xml);
?>

OUTPUT
SimpleXMLElement Object ( [to] => Ankita [from] => Pune [heading] => Php
Project )

(II) Php simpleXML-(Read from file)


The php simplexml_load_file() function is used to read XML data from file.
We have an xml file called note.xml.

Example
note.xml (create xml file)

<?xml version="1.0" encoding="UTF-8"?>


<note>
<to>Ankita</to>
<from>Pune</from>
<heading>Php Project</heading>
</note>

Filexml.php (create php file)


<?php
$xml=simplexml_load_file("note.xml") or die ("Error: cannot create object");

print_r($xml);
?>

OUTPUT
SimpleXMLElement Object ( [to] => Ankita [from] => Pune [heading] => Php Project )

---------------------------------------------------------------------------------------------------------------------------

Types of XML Parsers-


1. DOM
2. The simple XML extension
DOM (Document Object Model)
A DOM document is an object which contains all the information of an XML document. It is
composed like a tree structure. The DOM Parser implements a DOM API. This API is very
simple to use.
Features of DOM Parser
A DOM Parser creates an internal structure in memory which is a DOM document object and
the client applications get information of the original XML document by invoking methods
on this document object.
DOM Parser has a tree based structure.
Advantages
1) It supports both read and write operations and the API is very simple to use.
2) It is preferred when random access to widely separated parts of a document is required.
Disadvantages
1) It is memory inefficient. (consumes more memory because the whole XML document
needs to loaded into memory).
2) It is comparatively slower than other parsers.

SAX (Simple API for XML)


A SAX Parser implements SAX API. This API is an event based API and less intuitive.
Features of SAX Parser
It does not create any internal structure.
Clients does not know what methods to call, they just overrides the methods of the API and
place his own code inside method.
It is an event based parser, it works like an event handler in Java.
Advantages
1) It is simple and memory efficient.
2) It is very fast and works for huge documents.
Disadvantages
1) It is event-based so its API is less intuitive.
2) Clients never know the full information because the data is broken into pieces.

You might also like