PHP
19 BEST PHP IDE and Code Editors in 2021 [Free/Paid]
PHP is an open-source server-side scripting language that is used to develop static or dynamic web...
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-
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;
DOM XML is used to access and manipulate XML documents. It views the XML document as a tree-structure.
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.
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.
<?xml version="1.0" encoding="utf-8"?> <employees status = "ok"> <record man_no = "101"> <name>Joe Paul</name> <position>CEO</position> </record> <record man_no = "102"> <name>Tasha Smith</name> <position>Finance Manager</position> </record> </employees>
HERE,
Let’s now write the code that will read the employees XML document and display the results in a web browser. Index.php
<?php
$xml = simplexml_load_file('employees.xml');
echo '<h2>Employees Listing</h2>';
$list = $xml->record;
for ($i = 0; $i < count($list); $i++) {
echo '<b>Man no:</b> ' . $list[$i]->attributes()->man_no . '<br>';
echo 'Name: ' . $list[$i]->name . '<br>';
echo 'Position: ' . $list[$i]->position . '<br><br>';
}
?>HERE,
Assuming you saved the file index.php in phptus/xml folder, browse to the URL http://localhost/phptuts/xml/index.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.
<?php
$dom = new DOMDocument();
$dom->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,
Assuming you saved the file create_movies_list in phptuts/xml folder, browse to the URL http://localhost/phptuts/xml/create_movies_list.php
Click on movies_list_xml link
PHP is an open-source server-side scripting language that is used to develop static or dynamic web...
What is PHP Date Function? PHP date function is an in-built function that simplify working with...
What is Laravel? Laravel is an open-source web MVC framework for PHP. Laravel is a robust...
A Loop is an Iterative Control Structure that involves executing the same number of code a number...
PHP has a rich collection of built in functions for manipulating MySQL databases. In this...
Potential security threats They are basically two groups of people that can attack your system...