Adv. PHP Sybba (Ca) Chapter Iii - XML Ms. Pooja R. Kamble 3.1 Introduction XML
Adv. PHP Sybba (Ca) Chapter Iii - XML Ms. Pooja R. Kamble 3.1 Introduction XML
PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
3.1 Introduction XML
What is 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.
XML is widely used in the era of web development. It is also used to simplify
data storage and data sharing.
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.
In the real world, computer systems and databases contain data in incompatible
formats.
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.
Exchanging data as XML greatly reduces this complexity, since the data can be
read by different incompatible applications.
XML data is stored in text format. This makes it easier to expand or upgrade to
new operating systems, new applications, or new browsers, without losing data.
Different applications can access your data, not only in HTML pages, but also
from XML data sources.
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
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.
o XHTML
o WSDL for describing available web services
o WAP and WML as markup languages for handheld devices
o RSS languages for news feeds
o RDF and OWL for describing resources and ontology
o SMIL for describing multimedia for the web
HTML vs XML
There are many differences between HTML (Hyper Text Markup Language)
and XML (eXtensible Markup Language). The important differences are given
below:
5) HTML has its own You can define tags according to your
predefined tags. need.
What is a DTD?
A DTD defines the structure and the legal elements and attributes of an XML
document.
XML DTD
The purpose of a DTD is to define the structure and the legal elements and
attributes of an XML document:
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
Note.dtd:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
!DOCTYPE note - Defines that the root element of the document is note
!ELEMENT note - Defines that the note element must contain the
elements: "to, from, heading, body"
!ELEMENT to - Defines the to element to be of type "#PCDATA"
!ELEMENT from - Defines the from element to be of type "#PCDATA"
!ELEMENT heading - Defines the heading element to be of type
"#PCDATA"
!ELEMENT body - Defines the body element to be of type "#PCDATA"
Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
<body>Don't forget me this weekend!</body>
<footer>&writer; ©right;</footer>
</note>
An XML parser is a program that translates the XML document into an XML
Document Object Model (DOM) Object.
The XML DOM Object can then be manipulated using JavaScript, Python, and
PHP etc.
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:
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
To read and update, create and manipulate an XML document, you will need an
XML parser.
Tree-Based Parsers
Event-Based Parsers
Tree-Based Parsers
Tree-based parsers holds the entire document in Memory and transforms the
XML document into a Tree structure. It analyzes the whole document, and
provides access to the Tree elements (DOM).
This type of parser is a better option for smaller XML documents, but not for
large XML document as it causes major performance issues.
SimpleXML
DOM
Event-Based Parsers
Event-based parsers do not hold the entire document in Memory, instead, they
read in one node at a time and allow you to interact with in real time. Once you
move onto the next node, the old one is thrown away.
This type of parser is well suited for large XML documents. It parses faster and
consumes less memory.
SAX
1. DOM
2. SAX
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
DOM (Document Object Model)
Advantages
1) It supports both read and write operations and the API is very simple to use.
Disadvantages
A SAX Parser implements SAX API. This API is an event based API and less
intuitive.
Clients does not know what methods to call, they just overrides the methods of
the API and place his own code inside method.
Disadvantages
2) Clients never know the full information because the data is broken into
pieces.
XML DOM defines a standard way to access and manipulate XML documents.
We can modify or delete their content and also create new elements. The
elements, their content (text and attributes) are all known as nodes.
<TABLE>
<ROWS>
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
<TR>
<TD>A</TD>
<TD>B</TD>
</TR>
<TR>
<TD>C</TD>
<TD>D</TD>
</TR>
</ROWS>
</TABLE>
SimpleXML turns an XML document into a data structure you can iterate
through like a collection of arrays and objects.
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
Compared to DOM or the Expat parser, SimpleXML takes a fewer lines of code
to read text data from an element.
$myXMLData =
"<?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>";
Example
<?php
$myXMLData =
"<?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>";
Error Handling Tip: Use the libxml functionality to retrieve all XML errors
when loading the document and then iterate over the errors. The following
example tries to load a broken XML string:
Example
<?php
libxml_use_internal_errors(true);
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<document>
<user>John Doe</wronguser>
<email>[email protected]</wrongemail>
</document>";
$xml = simplexml_load_string($myXMLData);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
print_r($xml);
}
?>
The PHP simplexml_load_file() function is used to read XML data from a file.
Assume we have an XML file called "note.xml", that looks like this:
Example
<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
print_r($xml);
?>
SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] =>
Reminder [body] => Don't forget me this weekend! )
3.7 Changing a value with simple XML
PHP SimpleXML - Get Node Values
Get the node values from the "note.xml" in above example file is included:
Example
<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Tove
Jani
Reminder
Don't forget me this weekend!
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
Another XML File
Assume we have an XML file called "books.xml", that looks like this:
The following example gets the node value of the <title> element in the first and
second <book> elements in the "books.xml" file:
Example
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]->title . "<br>";
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
echo $xml->book[1]->title;
?>
Everyday Italian
Harry Potter
The following example loops through all the <book> elements in the
"books.xml" file, and gets the node values of the <title>, <author>, <year>, and
<price> elements:
Example
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
echo $books->title . ", ";
echo $books->author . ", ";
echo $books->year . ", ";
echo $books->price . "<br>";
}
?>
The following example gets the attribute value of the "category" attribute of the
first <book> element and the attribute value of the "lang" attribute of the <title>
element in the second <book> element:
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
Example
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]['category'] . "<br>";
echo $xml->book[1]->title['lang'];
?>
COOKING
en
PHP SimpleXML - Get Attribute Values - Loop
The following example gets the attribute values of the <title> elements in the
"books.xml" file:
Example
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
echo $books->title['lang'];
echo "<br>";
}
?>
en
en
en-us
en-us
Program:-
1:-Write a PHP script to create XML file named “Course.xml”
Store the details of 5 students who are in SYBBACA.
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
<Course>
<SYBBA CA>
<Student name> ABC</Student name>
<Class name> A Class</Class name>
<percentage>90 </percentage>
</SYBBA CA>
<SYBBA CA>
<Student name> XYZ</Student name>
<Class name> B Class</Class name>
<percentage>70 </percentage>
</SYBBA CA>
<SYBBA CA>
<Student name> PQR</Student name>
<Class name> C Class</Class name>
<percentage>50 </percentage>
</SYBBA CA>
<SYBBA CA>
<Student name> LMN</Student name>
<Class name> A Class</Class name>
<percentage>92 </percentage>
</SYBBA CA>
<SYBBA CA>
<Student name> YZW</Student name>
<Class name> A Class</Class name>
<percentage>85 </percentage>
</SYBBA CA>
</Course>
2:-Write PHP script to create a CD catalog using XML file.
Xml file :
<CD>
<cd type='music'>
<name>silent_songs</name>
<launch-date>5-6-2014</launch-date>
<composer>A-R-Rehman</composer>
</cd>
Adv. PHP
SYBBA (CA) Chapter III – XML Ms. Pooja R. Kamble
<cd type='music'>
<name>Hip-Hop</name>
<launch-date>4-8-2011</launch-date>
<composer>Yo-Yo-Honey singh</composer>
</cd>
<cd type='music'>
<name>love track</name>
<launch-date>6-9-2000</launch-date>
<composer>Arjit Singh</composer>
</cd>
</CD>
Php file :
<?php
$xml=simplexml_load_file('CD.xml');
var_dump($xml);
?>
3:- Create a XML file which gives details of books available in “ABC Bookstore”
from following categories
1) Technical
2) Cookin
3) YOGA
Xml file :
<title>Create XML</title>
</head>
<form action=book.php.php method=get>
Enter Book Title<input type=text name=txtTitle></br>
Enter Publication<input type=text name=publ></br>
<input type=submit value="submit">
</form>
</html>
PHP File
<?php
if($fp=fopen("books.xml","w"))
{
if($wt=fwrite($fp,$xmltags))
{
echo "File created";
}
else
echo "Not write";
}
else
echo "Not opened";
?>