---
title: "SimpleXML in PHP"  
description: "SimpleXML is an easy way of getting an element's attributes and text, if you know the XML document's layout. Compared to DOM or the Expat parser, Simp"  
author: "Anonymous User"  
published: 2011-09-23  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/741/simplexml-in-php  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# SimpleXML in PHP

SimpleXML is an easy way of getting an element's [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) and text, if you know the XML [document](https://www.mindstick.com/articles/328642/what-to-consider-while-choosing-a-software-documentation-tool)'s layout. Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read [text data](https://answers.mindstick.com/qa/112298/how-do-i-implement-sentiment-analysis-in-text-data) from an element.\

The SimpleXML [extension](https://www.mindstick.com/articles/22/extension-method) provides a very simple and easily usable toolset to convert XML to an object like this.

- Elements are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they're [placed inside](https://www.mindstick.com/forum/12672/binding-all-college-names-to-a-college-dropdownlist-based-on-data-selected-in-another-city-dropdownlist-both-dropdownlists-are-placed-inside-the-datalist) an array.
- Attributes are accessed using associative arrays, where an index corresponds to the [attribute](https://www.mindstick.com/blog/61/ado-dot-net-object-model-and-attributes) name.
- Element Data, text data from elements are converted to strings. [If an element](https://www.mindstick.com/forum/160320/how-you-can-check-if-an-element-exists-in-a-set-and-add-or-remove-elements-from-it) has more than one text node, they will be arranged in the order they are found.

SimpleXML is fast and easy to use when performing basic tasks such as reading [xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp), extracting data from xml string and editing text node. However, when dealing with [advanced](https://yourviews.mindstick.com/view/87321/the-top-10-advanced-countries-in-space-technology) XML, like [namespaces](https://www.mindstick.com/forum/155583/what-are-namespaces-in-c-sharp), you are better off using the Expat parser or the XML DOM.

##### Let’s have an example how to read xml file using with SimpleXML in PHP.

Example:

Let’s create an XML file.

```
<?xml version="1.0" encoding="ISO-8859-1"?><note>     <to> Amit  </to>     <from> Arun  </from>     <subject> XML Parser</subject>     <message> This is testing message on PHP XML parser</message></note>
```

Now save it as ‘TestXML.xml’.

```
<?php    //  load xml file with specified location    $loadXml=  simplexml_load_file("TestXml.xml");      //get the name of the first element    $name = $loadXml->getName();    echo 'First element of file : '.$name."</br>";       // display the child element     foreach ($loadXml->children() as $child)    {         echo '<b>'.$child->getName().'</b>'." : ".$child ;           echo "</br>";    }?>
```

##### Output:

![SimpleXML in PHP](https://www.mindstick.com/mindstickarticle/76aa614f-07b0-41ad-bcfe-358cfd143589/images/baad0967-3396-47ce-a902-58ec55ebe3b6.png)

\

---

Original Source: https://www.mindstick.com/articles/741/simplexml-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
