简体   繁体   中英

PHP SimpleXML replaceChild() Error: “Call to undefined method[…]”

I am running PHP5 on a Linux Apache Server

I am trying to use XML in my website as a simple database.

I need to raise an integer (the content of a node) by one, and save the file.

The Error Message is : Call to undefined method SimpleXMLElement::replaceChild()

Here is my code:

<?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>

replaceChild is a method of the DOMNode class, not simpleXML . SimpleXML does not provide any method to replace or remove a child, so you need to use dom_import_simplexml and then you can use the function replaceChild in your converted DOM object.

You have this:

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

... where $yes is a SimpleXMLElement object and such class does not have any replaceChild() method. I guess you want something like this instead:

$poll->yes++;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM