加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 替换SimpleXMLElement的值

发布时间:2020-12-13 17:05:17 所属栏目:PHP教程 来源:网络整理
导读:这可能很简单但我在网上找不到任何例子.我需要使用xpath找到一个节点并替换它的值. 这是xml文档的一个小版本: ?xml version="1.0" encoding="utf-16" standalone="yes"?w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
这可能很简单但我在网上找不到任何例子.我需要使用xpath找到一个节点并替换它的值.

这是xml文档的一个小版本:

<?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 Doe</w:t>
        </w:r>
    </w:p>
  </w:body>
</w:document>

这是我的PHP代码:

<?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();

?>

基本上,John Doe应该是George Dow

解决方法

我找到了解决方案.基本上,由于xpath函数返回一个SimpleXMLElement对象和一个数组,我需要将它作为一个访问:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读