Code has been added to clipboard!

Guide on Functions for PHP SimpleXML Parser and Manipulating XML Data

Reading time 3 min
Published Aug 8, 2017
Updated Oct 15, 2019

In our previous lessons, we have covered what exactly a PHP XML parser is and its usage. We also got familiar with PHP SimpleXML parser.

This extension is considered to be one of the most popular ways to make PHP read XML files because it is free to use and comes inbuilt in PHP since version 5. In this lesson, we provide you with a handy list of PHP SimpleXML functions.

PHP SimpleXML: Main Tips

  • Using a parser is necessary if you wish to make PHP read XML files.
  • SimpleXML is a PHP XML parser that comes inbuilt in PHP 5 and newer versions.
  • It is tree-based (as opposed to event-based), treating a document as an XML tree: a structured collection of arrays and objects.
  • This parser is very useful if you know the inner structure of the XML file you're using.

List of Functions

Carefully review the list of functions that are used to make PHP parse XML files. The table is organized in an alphabetical order, so you can always use it to quickly refer to something you need:

Function Description
__construct() Create new SimpleXMLElement objects
addAttribute() Add attributes to the SimpleXML elements
addChild() Add child elements the SimpleXML elements
asXML() Return properly formed XML strings (XML version 1) from SimpleXML objects
attributes() Return attributes/values of elements
children() Return children of specified nodes
count() Count children of specified nodes
getDocNamespaces() Returns namespaces that were DECLARED in document
getName() Return name of XML tags that were referenced by SimpleXML elements
getNamespaces() Return namespaces that were USED in document
registerXPathNamespace() Create namespace context for next XPath query
saveXML() Alias for the asXML() function
simplexml_import_dom() Return SimpleXMLElement objects from DOM nodes
simplexml_load_file() Convert XML files into SimpleXMLElement objects
simplexml_load_string() ConvertXML strings into SimpleXMLElement objects
xpath() Run XPath queries on XML data

Iteration Methods

There are also a few iteration functions that you can use in PHP parsing XML files. Have a look at how they're called and what they do:

Function Description
current() Return current element
getChildren() Return child elements of current element
hasChildren() Check if current element has children
key() Return current key
next() Move toward next element
rewind() Rewind toward first element
valid() Check if current element is not invalid

PHP SimpleXML: Summary

  • Parsers are needed to read XML files.
  • PHP 5 and all the newer versions have an inbuilt XML parser called SimpleXML.
  • It treats XML documents as collections of arrays and objects, creating an XML tree-like map through it. Therefore it is called a tree-based parser.
  • If you wish to be parsing XML files that you know the structure of, PHP SimpleXML is a good choice.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()