【发布时间】:2014-12-11 14:43:21
【问题描述】:
我正在使用此代码来动态更新 xml 文件,
我的问题是我对如何应用要执行的操作的顺序感到困惑:
<?php
$files = scandir('./resources');
sort($files);
foreach($files as $file){
if(is_dir("./resources/$file") and $file != "." && $file != ".."){
myFun($file);
}
}
$simplexml = simplexml_load_file('map.xml');
function myFun($scannedTitle){
$tags = get_meta_tags('./resources/'.$scannedTitle.'/index.html');
$scannedDescription = $tags['description'];
$items = $simplexml->xpath('channel/item[@id="'.$scannedTitle.'"]');
$items = $items[0];
if($items->title[0] == $scannedTitle){
}else{
$items->title[0] = $scannedTitle;
}
if($items->description[0] == $scannedDescription){
}else{
$items->description[0] = $scannedDescription;
}
}
$simplexml->saveXML("map.xml");
?>
代码正常工作,但现在我更改了函数的顺序,它不再工作..
我不明白我必须按什么顺序执行这些操作...
这是我的“map.xml”文件:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<item id="red">
<title>red</title>
<link>http://website.com/resources/red/</link>
<description>descriptionRed</description>
</item>
<item id="green">
<title>green</title>
<link>http://website.com/resources/green/</link>
<description>Description of the green page.</description>
</item>
<item id="blue">
<title>blue</title>
<link>http://website.com/resources/blue/</link>
<description>Description of the blue page.</description>
</item>
</channel>
</rss>
【问题讨论】:
-
xml文件是否已经创建?因为我没有看到你测试它是否存在。