xml - 按日和月过滤 XML
问题描述
美好的一天,我想弄清楚如何过滤 xml 文件以显示当前月份和日期的项目。这有点像“历史上的这一天”,今天 14/10 将显示日期为 14/10 的 xml 内容,无论年份如何。该代码是我正在尝试自定义的 joomla 模块的一部分,因此我可以显示我的自定义历史事件。
亲切的问候
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<articles>
<article>
<url>/someurl</url>
<title>Title</title>
<text><![CDATA[Event info text]]></text>
<date>1914-10-14</date>
<date_publish>0</date_publish>
<image>/image.jpg</image>
</article>
<article>
<url>/someurl</url>
<title>Title</title>
<text><![CDATA[Event info text]]></text>
<date>1945-10-14</date>
<date_publish>0</date_publish>
<image>/image.jpg</image>
</article>
</articles>
php文件
class NSP_GK5_xml_file_Model {
// Method to get sources of articles
static function getSources($config) {
$content = array();
// if there are selected files - set the variables
if($config['xml_file'] != -1 && file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
// loading file content
$file_content = file_get_contents(JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk5' . DS . 'external_data' . DS . $config['xml_file']);
//
$xml = new SimpleXMLElement($file_content);
//
if(count($xml->article) > 0) {
//
$art = array();
//
foreach ($xml->article as $element) {
//
foreach($element as $key => $value) {
$art[$key] = (string) $value;
}
//
array_push($content, (array) $art);
}
}
}
//
return $content;
}
// Method to get articles in standard mode
static function getArticles($items, $config, $amount) {
$content = array();
//
for($i = $config['offset']; $i < $amount + $config['offset']; $i++) {
if(isset($items[$i])) {
array_push($content, $items[$i]);
}
}
// the content array
return $content;
}
}
// EOF
解决方案
使用 Xpath 表达式。日期应该是固定长度的格式。所以你可以摆脱字符串比较。
$month = '10';
$day = '14';
$expression = "//article[substring(date, 6, 5) = '$month-$day']";
$root = new SimpleXMLElement($xmlString);
$articles = [];
foreach ($root->xpath($expression) as $article) {
$articles[] = [
'title' => (string)($article->title ?? ''),
'date' => (string)($article->date ?? ''),
// ...
];
}
var_dump($articles);
你可能会注意到我明确地阅读了数据。通用(抽象)读取意味着您可能会错过结果中的键或携带您不使用的数据。它使您的源不太稳定。所以默认情况下要具体。
同样可以在 DOM 中实现:
$month = '10';
$day = '14';
$expression = "//article[substring(date, 6, 5) = '$month-$day']";
$document = new DOMDocument();
$document->loadXML($xmlString);
$xpath = new DOMXpath($document);
$articles = [];
foreach ($xpath->evaluate($expression) as $article) {
$articles[] = [
'title' => $xpath->evaluate('string(title)', $article),
'date' => $xpath->evaluate('string(date)', $article),
// ...
];
}
var_dump($articles);
推荐阅读
- php - 如何在laravel中为whereIn使用别名
- r - 如何在 r shiny 中制作自定义的可下载报告?
- reactjs - 如何在 react-router-dom 中限制通过 URL 直接访问
- javascript - React 新手,在 Modals 上苦苦挣扎。这是处理模态的正确方法吗?
- vue.js - VueJS 进程显示未定义
- javascript - 我试图在注销时清除 AsynStorage
- android - 从后台返回时,Android 的 Firestore 的查询快照侦听器需要很长时间(1 分钟)才能从网络更新
- python - 如何在 Django 中创建数据库模型?
- java - How to Prevent your Andriod app to minimize
- reactjs - React 状态未在 IE 中更新