开发者

Replace value of SimpleXMLElement

开发者 https://www.devze.com 2023-01-29 02:45 出处:网络
This may be simple but I couldn\'t find any example on the web. I need to find a node using xpath and replace it\'s value.

This may be simple but I couldn't find any example on the web. I need to find a node using xpath and replace it's value.

This is a small version of the xml document:

<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    </w:p>
        <w:r>
            <w:t>John Do开发者_JAVA百科e</w:t>
        </w:r>
    </w:p>
  </w:body>
</w:document>

And this is my php code:

<?php

$xml = simplexml_load_file("doc1/word/document.xml");
$result = $xml->xpath("/w:document/w:body/w:p[1]/w:r[1]/w:t[1]");

// the following code doesn't work...
$xml->$result = "George Dow";

echo $xml->asXML();

?>

Basically, John Doe should be George Dow


I found the solution. Basically, since the xpath function returns a SimpleXMLElement Object an array, I need to access it as one:

// the following code doesn't work...
$xml->$result = "George Dow";

// but this does :D
$result[0][0] = "George Dow";
0

精彩评论

暂无评论...
验证码 换一张
取 消