How to read XML file in PHP

 In this example show how to read xml file in PHP. Let say the XML file is like this :

<?xml version="1.0" encoding="utf-8"?>
<config userdb="custinfosys" dbpass="P@ssw0rd" host="localhost:3306" database="customerinfosys"/>

So i want to read config element and all of the attribute . Here is a sample code to read xml file :

PHP -> read XML File :

        //declare DOMDocument
        $objDOM = new DOMDocument();

        //Load xml file into DOMDocument variable

        //Find Tag element "config" and return the element to variable $node
        $node = $objDOM->getElementsByTagName("config");

        //looping if tag config have more than one
        foreach ($node as $searchNode) {
            $dbHost = $searchNode->getAttribute('host');
            $dbUser = $searchNode->getAttribute('userdb');
            $dbPass = $searchNode->getAttribute('dbpass');
            $dbDatabase = $searchNode->getAttribute('database');

NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example