We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15558
    • 108 Posts
    Hi there,

    For a real estate website, I'd need Modx to create one resource per item for all the items contained in a XML feed. Up to now I've never had the opportunity to work with XML stuff and have low PHP knowledge.

    Is there a - quite easy- way to do that ? Can someone teach me how to design that ? Thank you in advance.
      Sorry for that ⇧ , I'm no dev but I wanna try
      • 36926
      • 701 Posts
      You just want to create an XML file of all resources within your site?

      The RSS tut should help guide you in the right direction:
      http://rtfm.modx.com/display/ADDON/getResources.Building+a+RSS+feed


        • 15558
        • 108 Posts
        No bennyb, thanks, I must apologize for being so vague :

        The XML feed comes from an external provider.

        In fact, for each new product he has, my client fills a form on an external full-web application, which dispatches those new items via a XML feed onto several realty web platforms. The website I have to build must be one of those platforms.

        I have no clue about how to fetch that XML data and creating resources according to it, no clue about what to care about when the clients edits one of its goods in his webapp, but I'd be glad if I could learn... or just see the light. I need someone to clear the path for me because at this moment I'm totally blind...
          Sorry for that ⇧ , I'm no dev but I wanna try
          • 36926
          • 701 Posts
          Hey Kehezen,

          So you want to create a bunch of resources from an XML file. This would require some custom code and unfortunately won't be "quite easy"

          What exactly do you need to do? If you just need to display some info on a page thats pulled in from feed? Then there's the extra "getFeed" which will allow you to do this. Sorry the rtfm seems down at the moment so can't provide a link.

            • 15558
            • 108 Posts
            Quote from: bennyb at Mar 06, 2013, 07:12 PM
            the rtfm seems down at the moment

            Indeed.

            I assumed that wouldn't be that easy - I guess this would be easier if I only had to display a list of all the items, sadly each of the items has to get its own page/own resource. I thought FeedX might help, dreaming isn't that harmful. Phew. Hey thanks. I keep on listening.
              Sorry for that ⇧ , I'm no dev but I wanna try
              • 39404
              • 175 Posts
              stalemate resolution associate Reply #6, 11 years, 2 months ago
              Hi kehezen,

              Although it would involve programming, it would be an awesome learning experience.

              Here, at a really high level, is how you could do it. As a forewarning, none of this would be considered "best practices" but at some point in time, you just want to get it working.

              You could minimize the programming effort by creating only two resources: <ol><li>one with property information</li>
              <li>one with a list of links to properties.</li></ol>
              The first resource would be a page which loads in a file from some directory (which has your property information in HTML format) and then simply echos the thing you loaded.

              The second page would call that first resource along with something in the URL which contained a variable to the key of the property you want to display.

              That leaves the transformation from an XML file into many HTML files. This can be done with a combination of a PHP script (not a lot of lines of coding), a XSLT file (which would transform a property in XML format into HTML). This PHP script could be as a third resource which creates the content for resource 2 (the list of links) and the individual HTML files loaded in resource 1.

              Although this may seem confusing, it's actually not too bad from a programming point of view.

              To get more information do some Google searching on PHP XML Transformations using XSLT, creating a XSLT file, and then on the MODX side working with resources and snippets.

              I hope this helps.

              Tom
                • 15558
                • 108 Posts
                Many thanks Tom,

                This makes sense, however I won't be able to take such a project in charge for now, for schedule and quality and decency reasons. What I may choose is to outsource that part.

                I hope this thread might help the next rookie and - who knows - engender a whole extra by 2020. I'm going back to my Intuos.

                Feel free to share your point of view and your solutions below
                  Sorry for that ⇧ , I'm no dev but I wanna try
                • Hi Kehezen,

                  I might have what you need, in two seperate parts. Part one is: getting the XML file. For what I know, the existing packages only support default RSS feeds. So if your feed includes something like <price>39.90</price> it will not be processed.

                  As I need a similar application for importing events which include location data (lon/lat) and eventtime, I wrote a simple import snippet. Its not ready yet. But it already fetches a remote xml file (only one at the moment) and passes all the elements into placeholders:

                  Call the snippet:
                  [[!xmlparser? &source=`http://www.spiegel.de/schlagzeilen/tops/index.rss` &tpl=`itemTpl`]]


                  Snippet code:
                  # Snippet to read and parse XML input
                  # USAGE: [[!xmlparser? &source=`feed.rss` &tpl=`xmlTpl`]]
                  
                  $source = $scriptProperties['source'];
                  
                  if (empty($source)) {
                      $modx->log(modX::LOG_LEVEL_DEBUG,'[xmlparser] Empty source adress passed, aborting.');
                      return '';
                  }
                  
                  else {
                  
                  if ($xml = simplexml_load_string(file_get_contents($source))) {
                  
                  // you might want to uncomment this to have a look into the XML structure
                  // print_r ($xmlstr);
                  
                  $output = '';
                    // do this for every item inside the "channel" tags
                    // change this to i.e. $xml->products->product or whatever your xml looks like
                    foreach ($xml->channel->item as $item) {
                      foreach ($item->children() as $key => $value) {
                      // make nice time format for display
                      if ($key == 'pubDate') {
                       $value = strftime("%d.%m.%Y %H:%M:%S", strtotime($value));
                      }
                      // put every items elements into placeholders
                      $modx->setPlaceholder($key, $value);
                      }
                      // throw all those placeholders into the chunk
                      $output .= $modx->getChunk($scriptProperties['tpl']);
                    }
                  } else {
                      exit('Konnte '.$source.' nicht öffnen.');
                  }
                  
                  return $output;
                  }


                  My Chunk for those XML items:
                  <h4><a href="[[+link]]" title="[[+title]]">[[+title]]</a></h4>
                  <p><b>[[+pubDate:default=``]]</b></br>
                  [[+locationname:default=``]]</br>
                  [[+eventdate:default=``]][[+description:default=``]]</p>
                  


                  I know it's not a clean solution, but it works for me.

                  The second task is to change this code into something that creates the resources. I have to admit that I don't know how to prevent the snippet to create the same product twice. But you should try the above code first, then we take care of the resources.

                  Cheers,

                  Guido
                  • The code which I took the samples from is basically some snippet called "formit2resource". You can search for it here in the forums. Its ment to create resources from a form input (surprise!). I'll try and combine these two into one, but have no time to test it right now.

                    This should be your new snippet code. Not finished, but it shoud already do something.

                    # Snippet to read and parse XML input
                    # USAGE: [[!xmlparser? &source=`feed.rss` &tpl=`xmlTpl`]]
                     
                    $source = $scriptProperties['source'];
                     
                    if (empty($source)) {
                        $modx->log(modX::LOG_LEVEL_DEBUG,'[xmlparser] Empty source adress passed, aborting.');
                        return '';
                    }
                     
                    else {
                     
                    if ($xml = simplexml_load_string(file_get_contents($source))) {
                     
                    // you might want to uncomment this to have a look into the XML structure
                    // print_r ($xmlstr);
                     
                    
                      // do this for every item inside the "channel" tags
                      // change this to i.e. $xml->products->product or whatever your xml looks like
                      foreach ($xml->channel->item as $item) {
                        // create a new resource
                        $doc = $modx->newObject('modResource');
                        $doc->set('createdby', $modx->user->get('id'));
                    
                        // set the template of new resource which contains all the important TVs
                        $doc->set('template', '2');
                    
                        // no need to explain this
                        $doc->set('hidemenu', '0'); //hide from menu
                        $doc->set('published', '1'); //publicate at once
                    
                        // set all values for tvs and resource properties
                        foreach ($item->children() as $key => $value) {
                    
                        // make nice time format for display. German time format in this case.
                        // you might have to change this to get along with the MODX database time format  (if you want to have it at all)
                        if ($key == 'pubDate') {
                         $value = strftime("%d.%m.%Y %H:%M:%S", strtotime($value));
                        }
                    
                        // create an unique alias from the XML item title
                        if($key== 'title'){
                          $doc->set('pagetitle', $value);
                          //replace spaces with -
                          $alias = preg_replace('/[\' \']/' , '-' , $value);
                     
                          //remove non alpha and a common injection string
                          $alias = preg_replace('/[^a-z0-9\.\x0B\-]/i' , '' , $alias);
                     
                                                       //this is the standard revo regexp
                                                       // \0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\
                        }
                        $doc->set('alias', $alias);
                    
                        // save the resource before adding TVs
                        $doc->save();
                    
                        // put every items elements into their TVs 
                        if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field))) {
                            $tv->setValue($doc->get('id'), $value);
                            $tv->save();
                        }
                        }
                      }
                    } else {
                        exit('Konnte '.$source.' nicht öffnen.');
                    }
                    $modx->cacheManager->refresh();
                    return true;
                    }


                    This code will should create resources from the XML feed (every time it's called!). But I did not test it. So please dont use it in a productive environment smiley

                    Cheers,

                    Guido
                    • You're welcome.