Updated April 17, 2023
Introduction to XML Parsing php
XML is a document which consists of name, data, attribute and other tags. It always has the root tag and all other tags are present inside it data inside the XML present as the textual content. We can read the xml if we know its structure of. So php provide us SimpleXMl extension which helps us to read the xml file. By the use of SimpleXMl we can easily manipulate the xml as well. By the use of SimpleXML we can easily convert the xml into the readable object inside the code and iterate them using any data structure. In the coming section of the tutorial, we will have closer look at the syntax for creating xml and reading data out of it, for beginners to understand it better. In this topic, we are going to learn about XML Parsing php.
Installation xml parsing php
If we want to read the xml file in php then it the library is present inside the core php itself, we are not required to download or install any third-party dependency for this to work. We can make use of SimpleXML which is provided by php core, no installation or download of dependency is required in this case, hence php makes it easy to deal with xml parsing in the code.
How to Read XML using PHP?
To read the cml file we will make use of SimpleXML parser which is the part of the php core. In order to use this, we can simply provide the xml file and use its function to parse it without much hustle. But in order to continue with the xml reading by using the SimpleXML parser, we first have to understand the basic structure of the xml file. So in this section we will first discuss the basic structure then we will start reading it using SimpleXML, so let’s get started to see below;
<?xml version='1.0' encoding='UTF-8'?>
<child tag 1> one </child tag 1>
<child tag 2> two </child tag 2>
// and so on .. //
As we can see in the above syntax, we can create the xml which will contain the root element. Let’s take a closer look at the syntax in detail for better understanding see below;
1) First it will contain the version and the encoding part for the xml document.
2) followed by the root element, this is nothing but the parent class for all the objects present inside it.
3) After that we can define the child object which is present it, and also can contain the list or array to represent the list of data.
4) remember all the tags which are opening should be close at the proper place; else it will give us error.
5) if the xml structure is not correct then it will give us an error while parsing it to pojo object.
Now we can read the php xml file using the function provided by the SimpleXML parser, let’s take a look at the function in detail to understand it see below;
a) simplexml_load_string(): This function is used to read the xml in php which is provided by the SimpleXML parser. This function has taken xml as the argument after that it will try to parse it. So we can pass our variable which is holding this xml inside the variable.
See the reference code for better understanding see below;
This way we can use this to parse our xml data.
How to Create XML using PHP?
In this section, we will see how we can create the XMl file using php SimpleXml parser, let take a closer look at it. For this we can simply have a variable that can hold the xml document for this below code will help you understand this, see below;
$varibale_name = " xml content here ";
As you can see in the above syntax we are trying to hold the xml content inside the variable for this we will also see one reference code to create it.
1) create the variable which will hold the xml
2) create xml as have discussed in the previous part with the root element, attribute, and information about the elements as well.
3) Every opening tag will have a closing tag as well, if not error will occur.
"<?xml version='1.0' encoding='UTF-8'?>
<state> MH </state>
<title> Hello </title>
<content> some information here ..// </content>
In this way, we can create the xml which is easy to use and create. In the next section, we will have closer look at the practice example using SimpleXml in php and run the program for better understanding with the output attached.
PHP SimpleXML – Read From File
In the previous section as we have discussed the creating and reading of the file using SimpleXML, in this section, we will have closer look at the practice example for better understanding. Let’s taken a look at it see below;
1) in the below example we will see the creation and reading of a file that is xml from the SimpleXMl parser provided by php core. We have used its function to read the file, bypassing our xml as the argument here. At the end we reprinting the result of xml,
// preparing the xml content for document.
"<?xml version='1.0' encoding='UTF-8'?>
<city> Mumbai </city>
<state> MH </city>
<title> Hello world!! </title>
<content> Information here ..// /< content >
// reading the file using SimpleXML load string function by passing the value.
// printing the result here ,//
In PHP, it is easy to parse the xml content because it provides us with a library which is SimpleXML and it is present inside the core php only, so we are not required to install or download any library externally to make this function work. It is easy to use, develop, readable, and maintainable as well by the developers.
This is a guide to XML Parsing PHP. Here we discuss the syntax for creating xml and reading data out of it, for beginners. You may also have a look at the following articles to learn more –