When exploring XML, you may have thought about where and how you would use it. I will try to answer the question of where and how we use it gradually.

Let's proceed over the library, as we started with examples.

Now we will use the following structure as an example XML.

<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book isbn="9759954949">
		<title>Yunus Emre Divanı</title>
		<author role="Compiler">Selim Yağmur</author>
		<language>Turkish</language>
		<edition>8</edition>
		<date>
			<year>2014</year>
			<month>04</month>
			<day>01</day>
		</date>
	</book>
	<book isbn="9753386203">
		<title>Risaletü'n-Nushiyye Yunus Emre</title>
		<author role="Translator">Prof. Dr. Umay Türkeş Günay</author>
		<language>Turkish</language>
		<edition>3</edition>
		<date>
			<year>2009</year>
			<month>01</month>
			<day>01</day>
		</date>
	</book>
</books>

Let's create the XML Tree structure with DOMDocument Class. Let's elaborate line by line on the code.

<?php
$xml = new DOMDocument(); // We create an object
$xml->encoding = 'UTF-8'; // We set the object's encoding to UTF-8

$books = $xml->createElement('books'); // We create a root element named "books"

$book = $xml->createElement('book'); // We create a "book" element
$book_isbn = $xml->createAttribute('isbn'); // We create an "isbn" attribute for the book element
$book_title = $xml->createElement('title'); // We create a "title" element
$book_author = $xml->createElement('author'); // ***
$book_role = $xml->createAttribute('role');
$book_language = $xml->createElement('language');
$book_edition = $xml->createElement('edition');
$book_date = $xml->createElement('date');

$book_date_year = $xml->createElement('year'); // ***
$book_date_month = $xml->createElement('month');
$book_date_day = $xml->createElement('day');

$book_date->appendChild($book_date_year); // We add the "year", "month", "day" elements under the "date" element.
$book_date->appendChild($book_date_month);
$book_date->appendChild($book_date_day);

$book_author->appendChild($book_role); // We add the "role" attribute to the "author" element
$book->appendChild($book_title); // We add "title", "author", "language" etc. under the "book" element.
$book->appendChild($book_author); // ***
$book->appendChild($book_language);
$book->appendChild($book_edition);
$book->appendChild($book_date);
$book->appendChild($book_isbn);

$books->appendChild($book); // We add the "book" element under the "books" element
$xml->appendChild($books); // We add the "books" element under the XML

$xml->save('./library.xml'); // We save the created XML object
?>

If we look at the library.xml file we created above;

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book isbn="">
	<title/>
	<author role="">
		<language/>
		<edition/>
		<date>
		  <year/>
		  <month/>
		  <day/>
	  </date>
  </author></book>
</books>

We have created the Library XML Tree structure.

In my next article, I will try to cover adding, reading, editing, deleting, and updating content in the created XML.