🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

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
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

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.