PHP的SimpleXML的replaceChild()错误:“调用未定义的方法[...]”

问题描述:

我一个Linux Apache服务器PHP的SimpleXML的replaceChild()错误:“调用未定义的方法[...]”

我想使用XML在我的网站作为一个简单的数据库上运行PHP5。

我需要将整数(节点的内容)加1,然后保存该文件。

的错误信息是:电话未定义的方法的SimpleXMLElement ::的replaceChild()

这是我的代码:

<?php 
$x = $_GET['x']; 
$y = $_GET['y']; 
$z = $_GET['z'];   
$dom = simplexml_load_file("questions.xml"); 
if ($dom->question['id'] == $x) { 
    $poll = $dom->question->poll; 
    if ($z == 0) { 
     $yes = $poll->yes; 
     $upper = $yes + 1; 
     $yes->replaceChild($yes, $upper); 
    } elseif ($z == 1) { 
     $no = $poll->no; 
     $lower = $no + 1; 
     $no->replaceChild($no, $lower); 
    } 
} 
?> 

questions.xml

<?xml version="1.0" encoding="utf-8" ?> 
<questions> 
    <question id="does-god-exist"> 
    <poll> 
     <yes>0</yes> 
     <no>0</no> 
    </poll> 
    </question> 
</questions> 
+0

你可以使用'DOMDocument :: load():从文件加载XML,然后继续。 – SparKot

replaceChild是一个方法DOMNode类,而不是simpleXMLSimpleXML不提供任何方法来替换或删除孩子,因此您需要使用dom_import_simplexml,然后您可以在转换的DOM对象中使用函数replaceChild

+0

然后会是什么解决方案?有没有简单的XML库的替代品? –

+0

是的,您可以使用dom_import_simplexml将您的simpleXML对象转换为DOM对象 – m4t1t0

你有这样的:

$yes = $poll->yes; 
$upper = $yes + 1; 
$yes->replaceChild($yes, $upper); 

...其中$yesSimpleXMLElement对象,这样的类没有任何replaceChild()方法。我想你想要这样的代替:

$poll->yes++;