Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get data from XML using SimpleXML extension in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 205
    Comment on it

    SimpleXML is an extension of PHP that allows us to get XML data.SimpleXML provides a way of getting element's name, attributes and textual content.SimpleXML extension fetches the data from XML document in a form of data structure.It is easy and best way to get data from XML because it takes a fewer lines of code to read text data from an element.

    In cakePHP we can get this done in an easy way by adding few lines of code in controller and view file

    You can use this code in controller for calling the SimpleXML extension for reading the data from any XML URL.

    public function news() { 
    
        $newData = array();
        $newData = simplexml_load_file('https://www.abcd.com/abcd.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
        $this->set("newsData", $newData);
    
        }
    


       After calling xml file in controller , we have to call the data from xml file . It is used to display the data which we want to show in a page.We have to add this code for fetching the data from XML file in the ctp file.

     

     <?php if (count($news->channel->item) > 0) {?>  // defining items
                <?php foreach($news->channel->item as $item) {?>  // loop for calling multiple news posts
                        <h3><a href="<?php echo $item->link?>"><?php echo $item->title?></a></h3> // title from xml file
                        <h5><?php echo $item->pubDate?></h5> // published date from xml file
                        <p><?php echo $item->description?></p> // description from XML file
                <?php }?>
            <?php }?>

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: