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"?>
<database>
<config userdb="custinfosys" dbpass="P@ssw0rd" host="localhost:3306" database="customerinfosys"/>
<defaultPassword></defaultPassword>
</database>


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
        $objDOM->load("../configuration.xml");

        //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');
        }


By
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

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example