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