What Is XML

What Is XML?

The name XML comes from the full name of the language, Extensible Markup Language. Although markup is in the name of the language, do not think of XML as you do HTML because, aside from the fact that both languages are based on tag pairs, there are no similarities. XML is used for the storage and exchange of data within its tag pairs, whereas HTML couldn’t care less what is contained in the content or how it is structured its only purpose is to display the content to the browser. In other words, XML is used to define and carry the content, whereas HTML is used to make it visually appealing to the reader.

ارایه درگاه پرداخت ePayBank.ir برای برنامه نویسان و وبمستران سایتها و وبلاگهای ایرانی

با درج آگهی در MyCityAd.ir میتوانید ویترین فروشگاهی داشته باشید و صفحه آگهی اختصاصی

با تهیه بسته آموزش طراحی سایت اختصاصی php از webdesign.epaybank.ir سایت خودتان را میتوانید براحتی برنامه نویسی و توسعه دهید تا در فضای اینترنتی خدماتتان را ارایه دهید.

Basic XML Document Structure

XML documents contain two major elements, the prolog and the body. The prolog contains the XML declaration statement, and any processing instructions and comments you want to add.

By the Way

For a complete definition of XML documents, read the XML specification at http://www.w3.org/TR/REC-xml.

The following snippet is a valid prolog:

<?xml version="1.0" ?>
<!-- Sample XML document -->

After the prolog comes the content structure. XML is hierarchical, like a bookbooks have titles and chapters, each of which contain paragraphs, and so forth. There is only one root element in an XML document. Continuing the book example, the element might be called Books, and the tags <Books></Books> surround all other information.


Next, add any subsequent elementscalled childrento your document. Continuing the book example, you will need a master book element and then within it elements for title, author, and publishing information. Call these child elements Title, Author, and PublishingInfo. But the publishing information will likely contain more than one bit of informationyou’ll need a publisher’s name, location, and year of publication. Not a problemjust create another set of child elements within your parent element (which also happens to be a child elements of the root element). For example, just the <PublishingInfo> element could look like this:

      <PublisherName>Joe SystemGod</PublisherName>
      <PublisherCity>Joe SystemGod</PublisherCity>
      <PublishedYear>Joe SystemGod</PublishedYear>

All together, a sample books.xml document with one entry could look something like this:

<?xml version="1.0" ?>
<!--Sample XML document -->
       <Title>A Very Good Book</Title>
       <Author>Jane Doe</Author>
           <PublisherName>Sams Publishing</PublisherName>

Keep in mind two important rules for creating valid XML documents:

  • XML is case-sensitive, so <Book> and <book> are considered different elements.
  • All XML tags must be properly closed, XML tags must be properly nested, and no overlapping tags are allowed.

Add some dummy entries to the books.xml file and place it in the document root of your web server for use in later examples. You will use the same XML file throughout the different interface examples shown in the rest of this chapter.

Accessing XML in PHP Using DOM Functions

The DOM XML extension has been part of PHP since version 4 but was completely overhauled in PHP 5. The primary change was to include the DOM functionality within a default installation of PHP, which is to say that no additional libraries or extensions need to be installed or configured to use these functions.

By the Way

DOM stands for Document Object Model. For more information on DOM, visit http://www.w3.org/TR/DOM-Level-2-Core/core.html.

The purpose of DOM functions is to allow you to work with data stored in an XML document using the DOM API. The most basic DOM function is DOMDocument->load(), which creates a new DOM tree from the contents of a file. After the DOM tree is created, you can use other DOM functions to manipulate the data.

In Listing 28.1, DOM functions are used to loop through a DOM tree and retrieve stored values for later display.

Listing 28.1. Loop Through an XML Document Using DOM Functions

1:  <?php
2:  $dom = new DomDocument;
3:  $dom->load("books.xml");
5:  foreach ($dom->documentElement->childNodes as $books) {
6:      if (($books->nodeType == 1) && ($books->nodeName == "Book")) {
8:          foreach ($books->childNodes  as $theBook) {
9:              if (($theBook->nodeType == 1) &&
10:             ($theBook->nodeName == "Title")) {
11:                 $theBookTitle = $theBook->textContent;
12:             }
14:             if (($theBook->nodeType == 1) &&
15:             ($theBook->nodeName == "Author")) {
16:                 $theBookAuthor = $theBook->textContent;
17:             }
19:             if (($theBook->nodeType == 1) &&
20:             ($theBook->nodeName == "PublishingInfo")) {
22:                 foreach ($theBook->childNodes as $thePublishingInfo) {
23:                     if (($thePublishingInfo->nodeType == 1) &&
24:                     ($thePublishingInfo->nodeName == "PublisherName")) {
25:                         $theBookPublisher = $thePublishingInfo->textContent;
26:                        }
28:                     if (($thePublishingInfo->nodeType == 1) &&
29:                     ($thePublishingInfo->nodeName == "PublishedYear")) {
30:                          $theBookPublishedYear =
31:                             $thePublishingInfo->textContent;
32:                        }
33:                 }
34:             }
35:         }
37:         echo "
38:         <p><span style=\"text-decoration:underline\">".$theBookTitle."</span>
39:         by ".$theBookAuthor."<br/>
40:         published by ".$theBookPublisher." in ".$theBookPublishedYear."</p>";
42:         unset($theBookTitle);
43:         unset($theBookAuthor);
44:         unset($theBookPublisher);
45:         unset($theBookPublishedYear);
46:     }
47: }

In line 2, a new DOM document is created, and in line 3, the contents of books.xml are loaded into this document. The document tree is now accessible through $dom, as you can see in later lines. Line 5 begins the master loop through the document tree, as it places each node of the document into an array called $books.

Line 6 looks for an element called "Book", and processing continues if one is found. Remember, the <Book></Book> tag pair surrounds each entry for a book in the books.xml file. If processing continues, line 8 gathers all the child nodes into an array called $theBook, and the if statements in lines 912 and 1417 look for specific nodes called "Title" and "Author", respectively, and place the values into the variables $theBookTitle and $theBookAuthor for later use.

Line 19 begins a similar if statement, but because this line looks for a node called "Publishing Info" and you know that the <PublishingInfo></PublishingInfo> tag pair contains its own set of child nodes, another looping construct is needed to obtain the information in the next level of data. On line 22, child nodes are found and placed in the array called $thePublishingInfo, and then if statements in lines 2326 and lines 2832 look for specific nodes called "PublisherName" and "PublishedYear", respectively, and place the values into the variables $theBookPublisher and $theBookPublishedYear for later use.

After the loop created in line 8 is closed in line 35, lines 3740 echo a marked-up string to the browser, using values stored in $theBookTitle, $theBookAuthor, $theBookPublisher, and $theBookPublishedYear variables. After these values are used, they are unset in lines 4245, and the loop continues for the next "Book" enTRy in the document tree.

Save this listing as domexample.php and place it in the document root of your web server. When viewed through your web browser you should see something like Figure 28.1.


For a complete listing of the more than 80 DOM-related functions in PHP, including functions to add to your XML document and save the new version, visit the PHP Manual at http://www.php.net/dom.

In the next section, you’ll use the same books.xml file but will retrieve and display its values using the SimpleXML family of functions.

Accessing XML in PHP Using SimpleXML Functions
SimpleXML is a new addition to PHP 5; it is enabled by default and requires no additional installation or configuration steps. It lives up to its description in the PHP Manual of being “a very simple and easily usable toolset to convert XML” while still being powerful.

Unlike the DOM family of functions, there are only a few SimpleXML functions and methodsseven, at current count. The most basic SimpleXML function parses the XML data into an object that can be directly accessed and manipulated without special functions to do so. The first function you need to know about is simplexml_load_file(), which loads a file and creates an object out of the data:

$object_with_data = simplexml_load_file(“somefile.xml”);

In Listing 28.2, a short bit of code is used to create a SimpleXML object and then display the hierarchy of the data stored in the object.

Listing 28.2. Load and Display Data Using SimpleXML
1: <?php
2: $theData = simplexml_load_file(“books.xml”);
3: echo “<pre>”;
4: print_r($theData);
5: echo “</pre>”;
6: ?>

In line 2, the contents of books.xml are loaded using simple_load_file() into an object called $theData. In line 4, the print_r() function is used to output a human-readable version of the data stored in the object, surrounded by the <pre></pre> tag pair.

Save this listing as simplexml_dump.php and place it in the document root of your web server. When viewed through your web browser you should see something like Figure 28.2.

Dumping out data isn’t all that spectacular, but it does show you the structure of the object, which in turn lets you know how to access the data in a hierarchical fashion. For instance, the output of simplexml_dump.php shows the entry for a book:

[0] => SimpleXMLElement Object
[Title] => A Very Good Book

[Author] => Jane Doe
[PublishingInfo] => SimpleXMLElement Object
[PublisherName] => Sams Publishing
[PublisherCity] => Indianapolis
[PublishedYear] => 2006

To reference this record directly, you would use


The elements in the record would be accessed like this:

$theData->Book->Title for the Title

$theData->Book->Author for the Author

$theData->Book->PublishingInfo->PublisherName for the Publisher Name

$theData->Book->PublishingInfo->PublisherCity for the Publisher City

$theData->Book->PublishingInfo->PublishedYear for the Published year

But because you likely would want to loop through all the records and not just the first one, the references to the data are a little different, as you can see in Listing 28.3.

Listing 28.3. Through an XML Document Using SimpleXML
1: <?php
2: $theData = simplexml_load_file(“books.xml”);
4: foreach($theData->Book as $theBook) {
5: $theBookTitle = $theBook->Title;
6: $theBookAuthor = $theBook->Author;
7: $theBookPublisher = $theBook->PublishingInfo->PublisherName;
8: $theBookPublisherCity = $theBook->PublishingInfo->PublisherCity;
9: $theBookPublishedYear = $theBook->PublishingInfo->PublishedYear;
11: echo ”
12: <p><span style=\”text-decoration:underline\”>”.$theBookTitle.”</span>
13: by “.$theBookAuthor.”<br/>
14: published by “.$theBookPublisher.” (“.$theBookPublisherCity.”)
15: in “.$theBookPublishedYear.”</p>”;
17: unset($theBookTitle);
18: unset($theBookAuthor);
19: unset($theBookPublisher);
20: unset($theBookPublishedYear);
21: }
22: ?>

In line 2, the contents of books.xml are loaded using simple_load_file() into an object called $theData. In line 4, the contents of $theData->Book, which is to say all the individual records, are put into an array called $theBook. Lines 59 gather the value of specific elements, beginning at the level of $theBook, and these values are output in lines 1115. Lines 1720 unset the value of the variables for the next pass through the loop.

Save this listing as simplexmlexample.php and place it in the document root of your web server.


This brief chapter introduced you to two sets of PHP functions used to manipulate XML: the DOM functions and the SimpleXML functions. In addition to a brief overview of both topics, examples of displaying information stored in XML files were shown, using each of these sets of functions. The overall purpose of this chapter was to introduce you to the concept of working with XML using PHP; there’s plenty more to learn on your own.