<item> <level_1>LEVEL1</level_1> <level_2> <child_level_2>LEVEL2</child_level_2> </level_2> </item>
<item> <item> <level_1>LEVEL1</level_1> <level_2> <child_level_2>LEVEL2</child_level_2> </level_2> </item> <item> <level_1>LEVEL1</level_1> <level_2> <child_level_2>LEVEL2</child_level_2> </level_2> </item> </item>
object(SimpleXMLElement)#33 (1) { ["item"]=> array(2) { [0]=> object(SimpleXMLElement)#26 (2) { ["level_1"]=> string(6) "LEVEL1" ["level_2"]=> object(SimpleXMLElement)#28 (1) { ["child_level_2"]=> string(6) "LEVEL2" } } [1]=> object(SimpleXMLElement)#27 (2) { ["level_1"]=> string(6) "LEVEL1" ["level_2"]=> object(SimpleXMLElement)#28 (1) { ["child_level_2"]=> string(6) "LEVEL2" } } } } array(1) { ["item"]=> string(7) " " }
<?php # Snippet to read and parse XML input # USAGE: [[!parsex? &source=`feed.rss` &tpl=`xmlTpl`]] # author: [email protected] # Let me know if you add or change things, maybe I can add them to the package in a later version! //$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); $offset = $modx->getOption('offset', $scriptProperties, 0); $debugmode = $modx->getOption('debugmode', $scriptProperties, false); $dataformat = $modx->getOption('dataformat', $scriptProperties, 'xml'); /*echo $dataformat; break; */ if (empty($source)) { $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] Empty source adress passed, aborting.'); return 'No source definded.'; } else { if ($dataformat == 'json') { if (!$json = file_get_contents($source)) { $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] can NOT read JSON file: '.$source); } if (!$obj = json_decode($json, true)) { $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] cant decode JSON file: '.$source); if (json_last_error() != JSON_ERROR_NONE) { $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] JSON error: '.json_last_error_msg()); } } // creating object of SimpleXMLElement $xmln = new SimpleXMLElement("<?xml version=\"1.0\"?><container></container>"); array_to_xml($obj,$xmln); $newxml = true; } if (($dataformat == 'xml')||($newxml==true)) { if ($newxml) { $xmldata = $xmln; $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] new XML object from JSON data '); //var_dump($xmldata); } else { $xmldata = file_get_contents($source); $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] XML data directly from remote source '); } if ($xml = simplexml_load_string($xmldata)) { $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] can read file: '.$source); $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] element value '.$element); // find parent element // select parent element // iterate children of parent element to receive elements containers attributes, too if (!$nodes = $xml->xpath("//$element")) { $modx->log(modX::LOG_LEVEL_ERROR,'[parseX] no nodes '); } $nodecount = 0; //echo 'Nodes: '.count($nodes).'<br />'; //echo $nodes->count(); foreach ($nodes as $node) { if ($offset > $nodecount) { $nodecount++; //$modx->log(modX::LOG_LEVEL_ERROR,'[parseX] '.$offset.' is greater than '.$nodecount ); continue; } else { if (($debugmode==true) && ($nodecount==0)) { //var_dump($node); } $values = array(); $attrib = $node->attributes(); foreach ($attrib as $attrkey => $attrval) { $values['.'.$attrkey] = (string)$attrval; } // $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($node)); 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); } function array_to_xml($obj, &$xmln) { foreach($obj as $key => $value) { if(is_array($value)) { if(!is_numeric($key)){ $subnode = $xmln->addChild("$key"); array_to_xml($value, $subnode); } else{ $subnode = $xmln->addChild("item$key"); array_to_xml($value, $subnode); } } else { $xmln->addChild("$key",htmlspecialchars("$value")); } } }
You might be better off pulling the data into a custom database table, unless it's constantly being updated.
If you set up the table and class for xPDO, you can use its query language to search for what you want.
If you import the data, you could store the month, date, and year in separate fields. Then a query for a particular month and date, regardless of year, would be very easy.
If you have the option to get (or convert) the incoming data as JSON or CSV, it might make the import much easier.
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `birthday_quotation` (
`id` int(25) unsigned NOT NULL auto_increment,
`quote` mediumtext NOT NULL,
`author` varchar(200) NOT NULL default '',
`topic` varchar(20) NOT NULL default '',
`birthday` varchar(20) NOT NULL default '',
`month` varchar(20) NOT NULL default '',
`date` varchar(20) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `topic` (`topic`),
KEY `author` (`author`),
KEY `month` (`month`),
KEY `date` (`date`),
KEY `birthday` (`birthday`),
FULLTEXT KEY `quote` (`quote`,`author`,`topic`,`birthday`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
<model package="quotes" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1"> <object class="BirthdayQuotes" table="birthday_quotation" extends="xPDOSimpleObject"> <field key="id" dbtype="int" precision="25" phptype="integer" null="false" default=""/> <field key="quote" dbtype="mediumtext" null="false" default=""/> <field key="author" dbtype="varchar" precision="200" phptype="string" null="false" default=""/> <field key="topic" dbtype="varchar" precision="20" phptype="string" null="true"/> <field key="birthday" dbtype="varchar" precision="20" phptype="string" null="false" default=""/> <field key="month" dbtype="varchar" precision="20" phptype="string" null="false" default=""/> <field key="date" dbtype="varchar" precision="20" phptype="string" null="false" default=""/> <field key="publishedby" dbtype="int" precision="25" phptype="integer" null="false" default="0" /> <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true"> <column key="id" collation="A" null="false" /> </index> <aggregate alias="Resource" class="modResource" local="resource_id" foreign="id" cardinality="one" owner="foreign" /> <aggregate alias="Creator" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" /> </object> </model>