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:
|__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|
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:
|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.
Latest Coupon Found:
TOP-RATED CODING COURSES
Learn the skills of tomorrow
The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!