Updating a node value with PHP's simpleXML and xpath not working

前端 未结 3 655
悲&欢浪女
悲&欢浪女 2020-12-07 04:43

I have the following code inside a class:

$tmp= $this->Xml->xpath(\"/page/text[@id=\'$this->Id\']\");
$tmp[0]= $this->Text;
echo $tmp[0];
echo $t         


        
相关标签:
3条回答
  • 2020-12-07 05:13

    Not found better way than this one:

    $dom=dom_import_simplexml($xml_element); // $xml_element - in your case is $tmp[0]
    $dom->nodeValue = "new value"; 
    
    0 讨论(0)
  • 2020-12-07 05:16

    PHP's docs are in dire need of an example of this... the answer seems to be:

    $tmp = $this->Xml->xpath("/page/text[@id='$this->Id']");
    $tmp[0][0] = $this->Text;
    echo $tmp[0][0];
    echo $this->Xml->asXml();
    $this->Xml->asXML($this->FileName); //save XML
    

    Seems you need to refer to the node's first child, which would be the text (I'm assuming this is what's going on anyway).

    Note the second brackets $tmp[0][0].

    0 讨论(0)
  • 2020-12-07 05:19
    list( , $node) = each($tmp);
    $node->asXML($this->FileName);
    
    0 讨论(0)
提交回复
热议问题