Monday, 16 December 2013

How to read XML file in PHP

Leave a Comment
 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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.