We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4385
    • 372 Posts
    I am working on a proof of concept for displaying hundreds of rates. An xml file is generated each week from another source. I created a snippet that parses it and formats the nodes with a template. Which is fine for the main rate page. But the site owner wants to be able to refer to certain rates throughout the site. Is there a better way? Should I use the api to update the chunks each week? I did add parameters to the snippet to just call a specific node or get all. Just curious if anyone had a better approach.

    This question has been answered by multiple community members. See the first response.

      DropboxUploader -- Upload files to a Dropbox account.
      DIG -- Dynamic Image Generator
      gus -- Google URL Shortener
      makeQR -- Uses google chart api to make QR codes.
      MODxTweeter -- Update your twitter status on publish.
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      Hmm.. I would imagine each rate to have a unique key?

      Based on the information you provide, I would suggest parsing the XML data into a cache file (likely as some associated array) and use a snippet that returns just the info you need from that cache file based on the unique key or something. If you have multiple pieces of information per rate, you could parse just that rate through a chunk in those scenarios.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 4385
        • 372 Posts
        Here is the snippet I came up with. I haven't used the cacheManager before. I am guessing that would speed up performance?

        This snippet can give me all the nodes formatted with a chunk, just one node, or just the value of an element.

        # Snippet to read and parse XML
        # [[!rateScrape? &source=`assets/RateSheetSavings.xml` &tpl=`rateDisplay` &itemNode=`21` &display=`ACCTRATE`]]
        # [[!rateScrape? &source=`assets/RateSheetSavings.xml` &tpl=`rateDisplay` &itemNode=`21`]]
        # [[!rateScrape? &source=`assets/RateSheetSavings.xml` &tpl=`rateDisplay`]]
        
        $source = $scriptProperties['source'];
        
        if (empty($source))
        {
          $modx->log(modX::LOG_LEVEL_DEBUG, 'Empty source address passed, aborting.');
          return '';
        }
        else
        {
          if ($xml = simplexml_load_string(file_get_contents($source)))
          {
            $output = '';
            $i=0;
            foreach ($xml->ROW as $item)
            {
              foreach ($item->children() as $key => $value)
              {
                $modx->setPlaceholder($key, $value);
                if ($scriptProperties['display'] == $key){$display = $value;}
              }
              if (isset($scriptProperties['itemNode']) && $scriptProperties['itemNode'] == $i)
              {
                $output = $modx->getChunk($scriptProperties['tpl']);
                  if ($scriptProperties['display'] != ""){$output = $display;}
                break;
              }
              ++$i;
              $output .= $modx->getChunk($scriptProperties['tpl']);
            }
          }
          else
          {
            exit('Could not open ' . $source . '.');
          }
          return $output;
        }
        
        



        Here is the xml...
         <ROW>
          <MJCD>SAV</MJCD>
          <SEQNBR>0</SEQNBR>
          <SEQBAL>0</SEQBAL>
          <PRODDESC>Regular Savings</PRODDESC>
          <ACCTRATE>.050%</ACCTRATE>
          <ACCTYIELD>.05%</ACCTYIELD>
          <MINFORYIELD>$0.00</MINFORYIELD>
          <MAXFORYIELD>$999,999,999.99</MAXFORYIELD>
          <RATECHGFREQ>Variable</RATECHGFREQ>
          <COMPPERIOD>Daily</COMPPERIOD>
          <EARNPERIOD>End Of Month</EARNPERIOD>
         </ROW>
        
          DropboxUploader -- Upload files to a Dropbox account.
          DIG -- Dynamic Image Generator
          gus -- Google URL Shortener
          makeQR -- Uses google chart api to make QR codes.
          MODxTweeter -- Update your twitter status on publish.
        • discuss.answer
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Using the cacheManager is pretty simple, and I do think that would be faster than parsing the XML on each request. See http://rtfm.modx.com/display/revolution20/Caching#Caching-UsingmodCacheManager

          If you want to properly parse placeholders in a chunk, I'd advise using it like this instead:

          $placeholders = array(
            'foo' => 'bar',
            'fooooo' => 'baaaar',
          );
          
          $output = $modx->getChunk('chunkname', $placeholders);


          $modx->setPlaceholder has the downside of making the placeholder global, so it might overwrite stuff or itself.

            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • discuss.answer
            • 3749
            • 24,544 Posts
            You might also consider this for parsing the whole XML file into an array:

            http://php.net/manual/en/function.xml-parse-into-struct.php


            Then you could build the array Mark is talking about from the array set by the function.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 4385
              • 372 Posts
              Thanks guys, that's what I needed.

              I think I will split it into two snippets. One that is run each week to read the XML files and store them in cache files. Then the other to check and display the data from each cache file.
                DropboxUploader -- Upload files to a Dropbox account.
                DIG -- Dynamic Image Generator
                gus -- Google URL Shortener
                makeQR -- Uses google chart api to make QR codes.
                MODxTweeter -- Update your twitter status on publish.