Last updated on August 14th, 2019 at 11:03 pm

Are you creating a website or web application where you need to get data from remote servers or some other application, then XML is one of the best way to get information from remote servers and applications.
IN PHP5’s simpleXML functions dramatically simplify the process of interpreting the feeds into something useful for your web pages.

phpxml

I recently work on a project where we have to get data from other source. XML is not a last option to get this thing done for you. You also use JSON to do that thing for you but i like XML simply so i use this.

Download

First we understand what is XML?

XML is eXtensible Markup Language is a way to structure your data for sharing across sites. Some of the technologies that are crucial to the web like RSS (Real Simple Syndication) and Podcasts are special flavors of XML. The beautiful thing about XML is that you can easily roll your own for anything you need.

As in definition you don’t need to read  a special book for that but need a little practise and you ready you easily mention you code or create your own tags.

XML is really a good but we have to remember something when we are creating a document in XML

  • XML is case sensitive.
  • You must close XML tag when you start.
  • XML requires a root element (the tag above serves as our root element)
  • Attributes must be quoted
  • Special characters (like & (&) and < (<) and > (>) signs) must be encoded.

Now we start making our first XML File with PHP

PHP XML

encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$dom->xmlStandalone = true;
$xml_file_name='test.xml';
$root = $dom->createElement('Users');
$user_node=$dom->createElement('User');
$attr_user_id = new DOMAttr('user_id', '1020');
$user_node->setAttributeNode($attr_user_id); 
$child_node_first_name=$dom->createElement('FirstName','Aneh');
$user_node->appendChild($child_node_first_name);
$child_node_last_name=$dom->createElement('LastName','Thakur');
$user_node->appendChild($child_node_last_name);
$child_node_email_address=$dom->createElement('EmailAddress','[email protected]');
$user_node->appendChild($child_node_email_address);
$child_node_address=$dom->createElement('Address');
$cdata_node = $dom->createCDATASection('Aneh Thakur Shimla, Himachal Pardesh');
$child_node_address->appendChild($cdata_node);
$user_node->appendChild($child_node_address);
$comment = $dom->createComment('this is a test comment');
$comment->appendData('add your custom text');
$dom->appendChild($comment);
$child_node_age=$dom->createElement('Age','12');
$attr = new DOMAttr('married', 'false');
$child_node_age->setAttributeNode($attr);
$user_node->appendChild($child_node_age);
$root->appendChild($user_node);
$dom->appendChild($root);
$dom->save($xml_file_name);

Now understanding above code

I call default class of PHP to create my new XML document

new DOMDocument();

Set some default parameter require to generate XML file

$dom->encoding = 'utf-8';

$dom->xmlVersion = '1.0';

$dom->formatOutput = true;

$dom->xmlStandalone = true;

Name the file i want to make eg {my.xml}

$xml_file_name='test.xml';

Create my root tag for xml file you also make your own root tag likeeg {, }

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

Create a child of root

$user_node=$dom->createElement('User');

$attr_user_id = new DOMAttr('user_id', '1020');

$user_node->setAttributeNode($attr_user_id);

And you also add attribute to your tag if you need that then follow above code. And if you like to add another child you do like this.

$child_node_first_name=$dom->createElement('FirstName','Aneh');

$user_node->appendChild($child_node_first_name);

$child_node_last_name=$dom->createElement('LastName','Thakur');

And if you want to add long string you need this

$child_node_address=$dom->createElement('Address');

$cdata_node = $dom->createCDATASection('Aneh Thakur Shimla, Himachal Pardesh');

$child_node_address->appendChild($cdata_node);

$user_node->appendChild($child_node_address);

And in the last you yo have to close your parent node and save the file

$dom->appendChild($root);
$dom->save($xml_file_name);

Now we end Creating or XML File so it is very important to read our created data using PHP.

Code:

$xml = simplexml_load_file("test.xml");
print_r($xml);

For more you follow this link.

Now if you need to get data from remote server you use this code to get the Remote XML Data.

$dom = new DOMDocument();

$dom->load('http://trinityblog.in/rss');

$dom->save('filename.xml');

Thanku!!

Happy Coding.

Posted in: php