简体   繁体   中英

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

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 $this->Xml->asXml();
$this->Xml->asXML($this->FileName); //save XML

the problem is "echo $tmp[0]" prints the updated text while $this->Xml->asXml() still contains the old node value. Searching around this website I found this to be the correct way to update the whole XML tree, yet this is still not working. Any idea?

Thanks

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] .

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"; 
list( , $node) = each($tmp);
$node->asXML($this->FileName);

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