相关疑难解决方法(0)

如何在不使用其父项的情况下设置SimpleXmlElement的文本值?

我想设置xpath()找到的某个节点的文本

<?php

$args = new SimpleXmlElement(
<<<XML
<a>
  <b>
    <c>text</c>
    <c>stuff</c>
  </b>
  <d>
    <c>code</c>
  </d>
</a>
XML
);

// I want to set text of some node found by xpath 
// Let's take (//c) for example

// convoluted and I can't be sure I'm setting right node
$firstC = reset($args->xpath("//c[1]/parent::*")); 
$firstC->c[0] = "test 1";

// like here: Found node is not actually third in its parent.
$firstC = reset($args->xpath("(//c)[3]/parent::*")); 
$firstC->c[2] = "test 2";

// following won't work for obvious reasons, …
Run Code Online (Sandbox Code Playgroud)

php xml xpath simplexml

21
推荐指数
2
解决办法
2万
查看次数

标签 统计

php ×1

simplexml ×1

xml ×1

xpath ×1