We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6506
    • 35 Posts
    Hmm, can't seem to get it to pull anything back. Here is the template

     <div class="fadeIn  portfolio-item mix [[+Date:strtotime:date=`%Y`]]">
                            <div class="service-two newsstuff [[+idx]] ">
                    <a href="[[+NewsRelease.ReleaseID]]">
                    <i class="icon wow fadeInLeft news"></i>
                    <h3>[[+Title:ellipsis=`30`]]</h3>
                    <span>NEWS ARTICLE ADDED  //  [[+Date:strtotime:date=`%m-%d-%Y`]]</span>
                    
                    <p>[[+Title:ellipsis=`75`]]</p>
                    </a>   
                </div><!-- end service -->
             </div><!-- end col-->               
    
    


    and here is the snippet call

    [[!parsex? &source=`http://xml.corporate-ir.net/irxmlclient.asp?compid=251258&reqtype=newsreleases_2` &limit=`5` &tpl=`newsarticles` &element=`NewsRelease` &wrapper=`wrapX`]]
    


    Do I have to come back out 1 level? When debugging it shows up.
      • 6506
      • 35 Posts
      Coming back out only then pulls back the first ReleaseID, yet there are about 30 or so articles that I need to pull out this ID for so I can display the full article.

      As you can see the xml is formed like this

      <IRXML CorpMasterID="251258">
      <NewsReleases PubDate="20150512" PubTime="21:05:53">
      <NewsRelease ReleaseID="2047271" DLU="20150512 21:00:31" ArchiveStatus="Current" RNSSource="" ContainerId="" Type="2">
      <Title>GasLog Ltd. Announces Election of Directors at 2015 Annual General Meeting of Shareholders</Title>
      <ExternalURL/>
      <Date Date="20150512" Time="17:00:31" TimeZone="ET">
      5/12/2015 5:00:31 PM
      </Date>
      <DisplayDateStart Date="20150512" Time="17:00:31">May 12 2015 05:00</DisplayDateStart>
      <DisplayDateEnd Date="20350512" Time="21:00:31">May 12 2035 09:00</DisplayDateEnd>
      <ContentNetworkingLinks/>
      <Categories>
        <Category>NA</Category>
      </Categories>
      </NewsRelease>
      <NewsRelease ReleaseID="2044578" DLU="20150506 11:23:15" ArchiveStatus="Current" RNSSource="" ContainerId="" Type="2">
        <Title>GasLog Ltd. Reports Financial Results for the Quarter Ended March 31, 2015</Title>
        <ExternalURL/>
        <Date Date="20150506" Time="07:23:15" TimeZone="ET">5/6/2015 7:23:15 AM</Date>
        <DisplayDateStart Date="20150506" Time="07:23:15">May 06 2015 07:23</DisplayDateStart>
        <DisplayDateEnd Date="20350506" Time="11:23:15">May 06 2035 11:23</DisplayDateEnd>
        <ContentNetworkingLinks/>
        <Categories>
          <Category>NA</Category>
        </Categories>
      </NewsRelease>
      <NewsRelease ReleaseID="2038092" DLU="20150421 11:00:39" ArchiveStatus="Current" RNSSource="" ContainerId="" Type="2">
        <Title>GasLog and GasLog Partners Announce the Time Charter of up to 9 Newbuildings to BG Group plc</Title>
        <ExternalURL/>
        <Date Date="20150421" Time="07:00:39" TimeZone="ET">4/21/2015 7:00:39 AM</Date>
        <DisplayDateStart Date="20150421" Time="07:00:39">Apr 21 2015 07:00</DisplayDateStart>
        <DisplayDateEnd Date="20350421" Time="11:00:39">Apr 21 2035 11:00</DisplayDateEnd>
        <ContentNetworkingLinks/>
        <Categories>
          <Category>NA</Category>
        </Categories>
      </NewsRelease>
      <NewsRelease ReleaseID="2037664" DLU="20150420 12:30:26" ArchiveStatus="Current" RNSSource="" ContainerId="" Type="2">
        <Title>GasLog Ltd. Announces Date for 2015 First Quarter Results</Title>
        <ExternalURL/>
        <Date Date="20150420" Time="08:30:26" TimeZone="ET">4/20/2015 8:30:26 AM</Date>
        <DisplayDateStart Date="20150420" Time="08:30:26">Apr 20 2015 08:30</DisplayDateStart>
        <DisplayDateEnd Date="20350420" Time="12:30:26">Apr 20 2035 12:30</DisplayDateEnd>
        <ContentNetworkingLinks/>
        <Categories>
          <Category>NA</Category>
        </Categories>
      </NewsRelease>
      
      </NewsRelease>
      </NewsReleases>
      </IRXML>
      


      and so on. I need each release ID so I an send it through to the next page. I have done it and it works I just need each ID pulling back. Any ideas? I'm nearly there. Thanks

      Danny
      • Ok, i see the problem. NewsRelease is the first element AND the container. So its children will be evaluated but nit its own attributes. I'll have a look.
          • 6506
          • 35 Posts
          Thanks, appreciate the help.
            • 6506
            • 35 Posts
            Anyone any ideas?

            Two things left to do. Get the ReleaseID and set an item to start on say item 2 or 3 rather than the first item.

            Really appreciate any help anyone can give.
            • New snippet code, including attributes of container element
              <?php
              # Snippet to read and parse XML input
              # USAGE: [[!parsex? &source=`feed.rss` &tpl=`xmlTpl`]]
              # author: [email protected]
              
              //$modx->setDebug(true);
               
              $source = $modx->getOption('source', $scriptProperties, 'http://modx.com/feeds/latest.rss');
              $element = $modx->getOption('element', $scriptProperties, 'item');
              $tpl = $modx->getOption('tpl', $scriptProperties, 'xmlTpl');
              $wrapper = $modx->getOption('wrapper', $scriptProperties, 'wrapX');
              $limit = $modx->getOption('limit', $scriptProperties, 0);
              $debugmode = $modx->getOption('debugmode', $scriptProperties, false);
              
              if (empty($source)) {
                  $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] Empty source adress passed, aborting.');
                  return 'No source definded.';
              }
              
              else {
                  if ($xml = simplexml_load_string(file_get_contents($source))) {
                      #$modx->log(modX::LOG_LEVEL_ERROR,'[parseX] can read file: '.$source);
                      #$modx->log(modX::LOG_LEVEL_ERROR,'[parseX] element value '.$element);
              
                      
                      $nodes = $xml->xpath("//$element");
              
                      $nodecount = 0;
              
                      foreach ($nodes as $node)
                      {
                          if (($debugmode==true) && ($nodecount==0)) {
                              var_dump($node);
                          }
                          $values = array();
                          $attrib = $node->attributes();
               
                          foreach ($attrib as $attrkey => $attrval) {
                            $values['.'.$attrkey] = (string)$attrval;  
                          }
              
                          foreach($node as $key => $value)
                          {
                              if ($key == 'pubDate') {
                                      $value = strftime("%d.%m.%Y %H:%M:%S", strtotime($value));
                                  }
                              $values[$key] = (string)$value;
              
                              foreach ($node->$key->attributes() as $attrkey => $attrval) {
                                  $values[$key.'.'.$attrkey] = (string)$attrval;
                              }
                          }
                          if ($debugmode==true && $nodecount==0) {
                             var_dump($values);
                          }
                      $output .= $modx->getChunk($tpl, $values);
                      $nodecount++;
                      if (($nodecount >= $limit) && ($limit !=0)) break;
                      }
              
                  }
                  else {
                      $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] can NOT read file: '.$source);
                  }
              
              $result = array("result" => $output);
              return $modx->getChunk($wrapper, $result);
              }


              Call attribute with [[+.ResultID]] (the dot before the attributes name is important).
                • 6506
                • 35 Posts
                Hi Guido, that's fantastic, thank you.

                I actually got a solution also, very similar. I've posted it below. Yours is better I think as it will pull out all the attributes which you can then use, mine was specific to the RealeaseID. I've posted it below for comparison. Thanks again for all your help in this, proving once again what a great community MODX has.


                My solution


                <?php
                # Snippet to read and parse XML input
                # USAGE: [[!parsex? &source=`feed.rss` &tpl=`xmlTpl`]]
                # author: [email protected]
                 
                //$modx->setDebug(true);
                  
                $source = $modx->getOption('source', $scriptProperties, 'http://modx.com/feeds/latest.rss');
                $element = $modx->getOption('element', $scriptProperties, 'item');
                $tpl = $modx->getOption('tpl', $scriptProperties, 'xmlTpl');
                $wrapper = $modx->getOption('wrapper', $scriptProperties, 'wrapX');
                $limit = $modx->getOption('setlimit', $scriptProperties, 0);
                $debugmode = $modx->getOption('debugmode', $scriptProperties, false);
                
                if (empty($source)) {
                    $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] Empty source adress passed, aborting.');
                    return 'No source definded.';
                }
                 
                else {
                    if ($xml = simplexml_load_string(file_get_contents($source))) {
                        #$modx->log(modX::LOG_LEVEL_ERROR,'[parseX] can read file: '.$source);
                        #$modx->log(modX::LOG_LEVEL_ERROR,'[parseX] element value '.$element);
                        $nodes = $xml->xpath("//$element");
                
                        $nodecount = 0;
                
                        foreach ($nodes as $node)
                        {
                        $nodecount++;
                        $values = array();
                      $releaseId = null;
                           foreach ($node->attributes() as $attrKey => $attrVal) {
                                             if ($attrKey == 'ReleaseID') {
                                                $releaseId = (string)$attrVal;
                                                }
                                       }
                
                            foreach ($node as $key => $value) {
                                    if ($key == 'pubDate') {
                                        $value = strftime("%d.%m.%Y %H:%M:%S", strtotime($value));
                                    }
                                    $values[$key] = (string)$value;
                                   $values['ReleaseID'] = $releaseId;
                					
                                    foreach ($node->$key->attributes() as $attrkey => $attrval) {
                                        $values[$key.'.'.$attrkey] = (string)$attrval;
                                    }
                                }
                                if ($debugmode==true) {
                                    var_dump($values);
                                }
                                $output .= $modx->getChunk($tpl, $values);
                                  
                                 if ($nodecount >= $limit) break;
                        }
                 
                    }
                    else {
                        $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] can NOT read file: '.$source);
                    }
                 
                $result = array("result" => $output);
                return $modx->getChunk($wrapper, $result);
                }
                
                • Finally, I packed this into a new MODX Extra Version:
                  http://modx.com/extras/package/parsex
                    • 50938
                    • 2 Posts
                    Great stuff! But can't download it! Can you fix it?

                    And it's not searching throw extras-search.
                      • 50938
                      • 2 Posts
                      Otherway just copied snippet + xmlTpl chunk. But don't know whats next?

                      1. Added code tio the page [[!parsex? &source=`http://modx.com/feeds/latest.rss` &tpl=`xmlTpl`]]
                      2. xmlTpl chunk
                      ------------
                      <h4><a href="[[+link]]" title="[[+title]]">[[+title]]</a></h4>
                      <p><b>[[+pubDate:default=``]]</b></br>
                      [[+locationname:default=``]]</br>
                      [[+eventdate:default=``]][[+description:default=``]]
                      </p>
                      ------------
                      3. Nothing happens. Can you show the usage? What should be in the result?
                      4. My log file:
                      ------
                      [2015-08-19 09:52:01] (ERROR @ /index.php) [parseX] CAN read file: http://modx.com/feeds/latest.rss
                      [2015-08-19 09:52:01] (ERROR @ /index.php) [parseX] element value: item
                      -------

                      Thanks!