Search A2Z 24

Question : What is PHP SimpleXML?

Please explain with example

asked July 13, 2014

1 Answers

Dispalying 1 to 1 of 1 |
PHP SimpleXML handles the most common XML tasks and leaves the rest for other extensions.

SimpleXML is new in PHP 5.

The SimpleXML extension provides is a simple way of getting an XML element's name and text.

Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an XML element.

SimpleXML converts the XML document (or XML string) into 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 are placed inside an array
  • Attributes are accessed using associative arrays, where an index corresponds to the attribute name
  • Text inside elements is converted to strings. If an element 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 tasks like:

  • Reading/Extracting data from XML files/strings
  • Editing text nodes or attributes

Notes : However, when dealing with advanced XML, you are better off using the Expat parser or the XML DOM.

PHP SimpleXML Examples

Assume we have the following XML file, "note.xml":

<?xml version="1.0" encoding="UTF-8"?>
<body>Don't forget me this weekend!</body>

Now we want to output different information from the XML file above:

Example 1

Output keys and elements of the $xml variable (which is a SimpleXMLElement object):



Read More on w3schools.com

See comments (0) | Comment
Answered 4 Years ago
Dispalying 1 to 1 of 1 |

Write your Answer write code <pre class="brush: xml">---code---</pre>

Login as a member to access answering block !! click-here

Reveal Modal Goodness

This is a default modal in all its glory, but any of the styles here can easily be changed in the CSS.

This is just a simple modal with the default styles, but any type of content can live in here. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi quis sem vel enim eleifend tristique. Etiam tincidunt faucibus pharetra.