Create An XML Document using PHP

0

How Can You Create An XML Document using PHP

If you want to understand the basic process of creating an XML document, follow the next example. We will try to create a simple XML structure using the DOM XML functions. Here are the steps:

  1. Create a new XML object in the cache memory.
  2. Create and add a few elements, an attribute, and a string. For this, we will need a HTML tag structure.
  3. The data will be saved in an .xml file called example_dom.xml on the server. PHP must have the permission to write on the server.

 

<?php$doc = new DomDoc(‘1.0’, ‘utf-6’);

// A new DOM document is created

$root = $doc->createElement(‘html’);

// The first element called the root is created

$root = $doc->append(root);

// The structure of the document is added to the root

$body = doc->create Element(‘body’);

// Another element called ‘body’ is created

$body = $root->appendChild($body);

// ‘body’ is added as a child element to the structure

$body->setAttribute(‘bgcolor’, ‘#8effed8’);

// the characteristics for the second element (body) is created

$graff = $doc->createElement(‘p’);

// Another element called ‘p’ is created

$graff = body->append Child(graf);

// The element ‘p’ is added as a child for the ‘body’ element

$text = doc->createNode(‘A text for the content of the file’);

// A string element is created

$text = $graff->appendChild($text);

// The text content is added to the ‘p’ structure

// The structure is saved and the data is written in the $doc file

// Displays a message if the process is succesful

if($doc->save(“exemple_dom.xml”)) echo ‘The simple document was created’;

else  echo ‘Error: the example_dom.xml cannot be created’;

?>

 

If you are using this script in a file.php and you are calling it from the browser, the file “example_dom.xml will be created, and an affirmative message will be displayed.

If you want to see the result, you will have to open the file using a text editor (or a browser)

 

Example 2 – Modifying an XML doc using PHP

Once it was created, you already have an XML document. However, if you need it modified, or you need to add an element, you will have to follow the next example. We will use the document created in the first example, but we will modify the content from the tag element ‘p’ and another element called ‘div’ will be added in the same parent structure. The explanations can also be found in the code.

 

The steps are the next:

  1. You will create a new object in the memory and you will add the data from an existing XML doc.
  2. The elements would become nods for the added document.
  3. If the program would try to execute the element that we want added, it will modify the existing one, creating anew element with name and text that is added to the modified one.

 

<?php

$file = ‘example_dom.xml’;

// The path and the name of the file .xml

$doc = new DOMDocument();

// creates a new memory object

$doc->load($file);

// loads the $file in the newly created object

$get_elms = $doc->getElements (“*”);

// Takes all the elements (“*”) from the object

$nr_elms = $get_elms->length;

// Obtains the number of elements (Nodes) that were modified

Leave a Reply

Your email address will not be published. Required fields are marked *