谁能看到我的代码有什么问题?我得到的错误如下:
致命错误:在第 35 行的 /path/to/php/file.php 中的非对象上调用成员函数 saveXML()
基本上,我只是想更新一个 XML nodeValue,它会根据“AVAILABILITY”标签中设置的值更改 UI。
==================== 更新====================
XML 文件现在没有被 ('AVAILABILITY') 标记内的 ('STATUS') 节点的新值覆盖,我做错了什么吗?!
=================================================
XML 结构
<?xml version="1.0" encoding="UTF-8"?>
<WORK>
<AVAILABILITY>
<STATUS>0</STATUS> // Not being changed?!
</AVAILABILITY>
<SLIDE>
<ID>YY001</ID>
<TITLE>YourEdentity</TITLE>
<LINK>http://youredentity.campearce.co.uk</LINK>
<THUMB>your_ed.png</THUMB>
<CAPTION>This is YourEdentity</CAPTION>
</SLIDE>
</WORK>
PHP 脚本:
<?php
$url = "../content/slides.xml";
$xml = simplexml_load_file($url);
if(!file_exists($url))
{
echo "Unable to locate file";
}
else
{
$root = $xml->WORK;
$slides = $root->SLIDE;
if(isset($_POST['action']) && !empty($_POST['action']))
{
$action = $_POST['action'];
switch($action)
{
case 'deleteslide' : delete_slide(); break;
case 'createslide' : create_slide(); break;
case 'changeavail' : change_avail($root, $_POST['status'], $xml, $url); break;
}
}
}
function change_avail($root_node, $status, $xml, $location){
$availability_node = $root_node->AVAILABILITY;
$status_node[0] = $availability_node->STATUS;
parse_str($status, $statustr);
$status_node[0] = $statustr;
file_put_contents($location, $xml->asXML());
echo $xml->asXML();
}
function save_toxml($xml, $location)
{
$fp = fopen($location, 'w');
fwrite($fp, $xml->asXML());
fclose($fp);
echo $xml->asXML();
}
?>