What is the xml in php?


What is XML?

The XML language is a way to structure data for sharing across websites.

Several web technologies like RSS Feeds and Podcasts are written in XML.

XML is easy to create. It looks a lot like HTML, except that you make up your own tags.

If you want to learn more about XML, please visit our XML tutorial.


What is an XML Parser?

To read and update, create and manipulate an XML document, you will need an XML parser.

In PHP there are two major types of XML parsers:

  • 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.

Example of tree-based parsers:

  • 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.

Example of event-based parsers:

  • XMLReader
  • XML Expat Parser


What is XML?

XML is the acronym for Extensible Markup Language.

XML is used to structure, store and transport data from one system to another.

XML is similar to HTML.

It uses opening and closing tags.

Unlike HTML, XML allows users to define their own tags.

In this tutorial, you will learn-

  • What is DOM?
  • XML Parsers
  • Why use XML?
  • XML Document Example
  • How to Read XML using PHP
  • How to Create an XML document using PHP

What is DOM?

DOM is the acronym for Document Object Model.

It’s a cross platform and language neutral standard that defines how to access and manipulate data in;

  • HTML
  • XHTML
  • XML

DOM XML is used to access and manipulate XML documents. It views the XML document as a tree-structure.

XML Parsers

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.

The keyword CDATA which is the acronym for (Unparsed) Character Data is used to ignore special characters such as “<,>” when parsing an XML document.

Why use XML?

  • Web services such as SOAP and REST use XML format to exchange information. Learning what XML is and how it works will get you competitive advantage as a developer since modern applications make heavy use of web services.
  • XML documents can be used to store configuration settings of an application
  • It allows you to create your own custom tags which make it more flexible.

XML Document example

Let’s suppose that you are developing an application that gets data from a web service in XML format.

Below is the sample of how the XML document looks like.




        

            Joe Paul

            CEO

        

        

            Tasha Smith

            Finance Manager

        

HERE,

  • “” specifies the xml version to be used and encoding
  • ” is the root element.
  • …” are the child elements of administration and sales respectively.

How to Read XML using PHP

Let’s now write the code that will read the employees XML document and display the results in a web browser. Index.php

Employees Listing';

$list = $xml->record;

for ($i = 0; $i < count($list); $i++) {

    echo 'Man no: ' . $list[$i]->attributes()->man_no . '
'; echo 'Name: ' . $list[$i]->name . '
'; echo 'Position: ' . $list[$i]->position . '

'; } ?>

HERE,

  • “$xml = simplexml_load_file(’employees.xml’);” uses the simplexml_load_file function to load the file name employees.xml and assign the contents to the array variable $xml.
  • “$list = $xml->record;” gets the contents of the record node.
  • “for ($i = 0; $i < count(…)…” is the for loop that reads the numeric array and outputs the results
  • “$list[$i]->attributes()->man_no;” reads the man_no attribute of the element
  • “$list[$i]->name;” reads the value of the name child element
  • “$list[$i]->position;” reads the value of the position child element

Testing our application

Assuming you saved the file index.php in phptus/xml folder, browse to the URL http://localhost/phptuts/xml/index.php

What is the xml in php?

How to Create an XML document using PHP

We will now look at how to create an XML document using PHP.

We will use the example above in the DOM tree diagram.

The following code uses the PHP built in class DOMDocument to create an XML document.

encoding = 'utf-8';

		$dom->xmlVersion = '1.0';

		$dom->formatOutput = true;

	$xml_file_name = 'movies_list.xml';

		$root = $dom->createElement('Movies');

		$movie_node = $dom->createElement('movie');

		$attr_movie_id = new DOMAttr('movie_id', '5467');

		$movie_node->setAttributeNode($attr_movie_id);

	$child_node_title = $dom->createElement('Title', 'The Campaign');

		$movie_node->appendChild($child_node_title);

		$child_node_year = $dom->createElement('Year', 2012);

		$movie_node->appendChild($child_node_year);

	$child_node_genre = $dom->createElement('Genre', 'The Campaign');

		$movie_node->appendChild($child_node_genre);

		$child_node_ratings = $dom->createElement('Ratings', 6.2);

		$movie_node->appendChild($child_node_ratings);

		$root->appendChild($movie_node);

		$dom->appendChild($root);

	$dom->save($xml_file_name);

	echo "$xml_file_name has been successfully created";
?>

HERE,

  • “$dom = new DOMDocument();” creates an instance of DOMDocument class.
  • “$dom->encoding = ‘utf-8’;” sets the document encoding to utf-8
  • “$dom->xmlVersion = ‘1.0’;” specifies the version number 1.0
  • “$dom->formatOutput = true;” ensures that the output is well formatted
  • “$root = $dom->createElement(‘Movies’);” creates the root node named Movies
  • “$attr_movie_id = new DOMAttr(‘movie_id’, ‘5467’);” defines the movie id attribute of Movies node
  • “$child_node_element_name = $dom->createElement(‘ElementName’, ‘ElementValue’)” creates the child node of Movies node. ElementName specifies the name of the element e.g. Title. ElementValue sets the child node value e.g. The Campaign.
  • “$root->appendChild($movie_node);” appends the movie_node elements to the root node Movies
  • “$dom->appendChild($root);” appends the root node to the XML document.
  • “$dom->save($xml_file_name);” saves the XML file in the root directory of the web server.
  • “echo ‘’ . $xml_file_name . ‘ has been successfully created’;” creates the link to the XML file.

Testing our application

Assuming you saved the file create_movies_list in phptuts/xml folder, browse to the URL http://localhost/phptuts/xml/create_movies_list.php

What is the xml in php?

Click on movies_list_xml link

What is the xml in php?

Summary

  • XML is the acronym for Extensible Markup Language
  • XML can be used for exchanging information between systems or store configuration settings of an application etc.
  • DOM is the acronym for Document Object Model. XML DOM views the XML document as a tree-structure
  • An XML Parser is a program that translates XML an XML document into a DOM tree-structure like document.
  • CDATA is used to ignore special characters when parsing XML documents.
  • PHP uses the simplexml_load_file to read XML documents and return the results as a numeric array
  • PHP DOMDocument class to create XML files.

Why XML is used in PHP?

XML is used to structure, store and transport data from one system to another. XML is similar to HTML. It uses opening and closing tags. Unlike HTML, XML allows users to define their own tags.

Whats is XML?

What is XML? XML stands for extensible markup language. A markup language is a set of codes, or tags, that describes the text in a digital document. The most famous markup language is hypertext markup language (HTML), which is used to format Web pages.

What is XML with example?

XML stands for Extensible Markup Language. It is a text-based markup language derived from Standard Generalized Markup Language (SGML). XML tags identify the data and are used to store and organize the data, rather than specifying how to display it like HTML tags, which are used to display the data.

What is XML and its function?

What is XML? The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. It was derived from an older standard format called SGML (ISO 8879), in order to be more suitable for Web use.