2021年5月6日星期四

Php: Finding a way to load xml once with simplexml

I'm using a class to load the appropriate nodes my xml into html through simplexml.

<?xml version="1.0" encoding="UTF-8"?>  <note>   <item>    <title>Reminder</title>   </item>   <item>    <title>Work</title>   </item>   <item>    <title>Play</title>   </item>   <item>    <title>Cycling</title>   </item>  </note>    <?php      class myxmlclass {              private $nodeno;        private $currenttitle;        private $myno;              //Load xml        function loadxm() {          $filename = 'xmlfile.xml';          if(file_exists($filename)){              $selxml=simplexml_load_file("xmlfile.xml", 'SimpleXMLElement', LIBXML_NOCDATA ) or die("Error: Cannot load xml");              htmlspecialchars($selxml);              return $selxml;          }        }  

When the page is loaded, the item is selected based a dynamic number set to $myno:

  $currentnode = $selxml->item[(int) $myno];    $currenttitle = $currentnode->title;  

The next and previous item is processed by a button click through ajax and the xml loads all over again.

I don't know how to code this so that the xml loads only once throughout the life of the session. Can someone show me the code to do that please?

https://stackoverflow.com/questions/67428508/php-finding-a-way-to-load-xml-once-with-simplexml May 07, 2021 at 11:06AM

没有评论:

发表评论