The Wayback Machine - https://web.archive.org/web/20240624222147/https://www.geeksforgeeks.org/how-to-parse-and-process-html-xml-using-php/
Open In App

How to parse and process HTML/XML using PHP ?

Last Updated : 17 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs.

The simplexml_load_string() function is used to parse the given XML and then the XML object can be used for accessing the XML data.

Example 1: The following example reads and prints the given XML string.

HTML




<html>
 
<body>
    <?php
    /* XML string */
    $myXMLData =
    "<?xml version='1.0' encoding='UTF-8'?>
    <note>
        <to>GeeksForGeeks</to>
        <from>Tom</from>
        <heading>Submission</heading>
 
        <body>
            Please see my articles of PHP!
        </body>
    </note>";
 
    /* The function reads xml data
       from the passed string */
    $xml = simplexml_load_string($myXMLData)
    or die("Error: Cannot create xml data object");
     
    print_r($xml);
    ?>
</body>
 
</html>


Output:

 SimpleXMLElement Object
 (
    [to] => GeeksForGeeks
    [from] => Tom
    [heading] => Submission
    [body] => Please see my articles of PHP!
 )

Example 2: The following example demonstrates how to get XML node values using PHP. The node values are shown in the output given below.

HTML




<!DOCTYPE html>
<html>
 
<body>
    <h2>GeeksForGeeks </h2>
    <b>
        Get data from XML data string
    </b>
     
    <br /><br />
    <?php
    // XML string
    $myXMLData =
        "<?xml version='1.0' encoding='UTF-8'?>
     
    <note>
        <to>GeeksForGeeks</to>
        <from>John</from>
        <heading>Submission</heading>
 
        <body>Please see my articles of PHP!</body>
    </note>";
 
    /* The function reads xml data
       from the passed string */
    $xml = simplexml_load_string($myXMLData)
    or die("Error: Cannot create xml data object");
    echo "To : " . $xml->to . "<br>";
    echo "From : " . $xml->from . "<br>";
    echo "Subject : " . $xml->heading . "<br>";
    echo "Mail : " . $xml->body;
    ?>
</body>
 
</html>


Output:

 



Similar Reads

How to parse a JSON File in PHP ?
In this article, we are going to parse the JSON file by displaying JSON data using PHP. PHP is a server-side scripting language used to process the data. JSON stands for JavaScript object notation. JSON data is written as name/value pairs. Syntax: { "Data":[{ "key":"value", "key":value, "key n ":"value" }, . . . . . . { "key":"value", "key":value,
2 min read
How to parse HTML file in PHP ?
In this article, we will learn to parse HTML in PHP. What is parsing? Generally parsing is converting one data type to another. It means how we can convert various data types to HTML. For example: Converting string to HTML. Why do we need parsing? To add the dynamic data (HTML content) at a certain point in PHP code, we need parsing. For example: F
2 min read
How to parse a CSV File in PHP ?
In this article, we learn to parse a CSV file using PHP code, along with understanding its basic implementation through examples.   Approach: The following approach will be utilized to parse the CSV File using PHP, which is described below: Step 1. Add data to an Excel file. The following example is given for sample data having Name and Coding Scor
3 min read
How to parse URL using JavaScript ?
Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method to create a HTML element, anchor tag and then use
2 min read
How to universally parse JSON into blocks using jQuery ?
In jQuery, to parse any data to any block is carried over by using DOM Insertion methods. Some of DOM Insertion methods are append(), appendTo(), html(), prepend(), prependTo(), text(). To parse JSON into any block is also handled in same way but along with Ajax callback functions and parse.JSON() methods. Here parse.JSON() methods is deprecated in
3 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
How to Parse JSON using Node.js?
JSON stands for JavaScript Object Notation. The text-based data exchange format data exchange format allows you to transfer data between different languages and platforms. JavaScript is commonly used to interact with JSON files. JSON parsing is a common task when working with data from APIs, configuration files, or databases. In this article, we wi
2 min read
Python | Parse a website with regex and urllib
Let's discuss the concept of parsing using python. In python we have lot of modules but for parsing we only need urllib and re i.e regular expression. By using both of these libraries we can fetch the data on web pages. Note that parsing of websites means that fetch the whole source code and that we want to search using a given url link, it will gi
2 min read
How to parse a URL into hostname and path in javascript ?
We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the current web page. C/C++ Code &lt;body style=&quot;
1 min read
What is the difference between the $parse, $interpolate and $compile services?
In AngularJS applications, the $parse, $interpolate and $compile services are the most important components as these services are used for data binding. Here, the $parse is mainly responsible for parsing and evaluating the expressions in the context of the application's scope. The $interpolate handles the interpolation of expressions and the $compi
6 min read
three90RightbarBannerImg