Viewed   87 times

I want to set text of some node found by xpath()


$args = new SimpleXmlElement(

// 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, 
// some setText() method would be perfect but I can't find nothing similar, 
$firstC = reset($args->xpath("//c[1]"));
$firstC = "test"; 

// maybe there's some hack for it?
$firstC = reset($args->xpath("//c[1]"));
$firstC->{"."} = "test"; // nope, just adds child named .
$firstC->{""} = "test"; // still not right, 'Cannot write or create unnamed element'
$firstC["."] = "test"; // still no luck, adds attribute named .
$firstC[""] = "test"; // still no luck, 'Cannot write or create unnamed attribute'
$firstC->addChild('','test'); // grr, 'SimpleXMLElement::addChild(): Element name is required'
$firstC->addChild('.','test'); // just adds another child with name .

echo $args->asXML();

// it outputs:
// PHP Warning:  main(): Cannot add element c number 2 when only 1 such elements exist 
// PHP Warning:  main(): Cannot write or create unnamed element 
// PHP Warning:  main(): Cannot write or create unnamed attribute 
// PHP Warning:  SimpleXMLElement::addChild(): Element name is required 
// <?xml version="1.0"? >
// <a>
//  <b>
//   <c .="test">test 1<.>test</.><.>test</.></c>
//   <c>stuff</c>
//  </b>
//  <d>
//   <c>code</c>
//  <c>test 2</c></d>
// </a>



You can do with a SimpleXMLElement self-reference:

$firstC->{0} = "Victory!!"; // hackity, hack, hack!
//  -or-
$firstC[0]   = "Victory!!";

found after looking at

var_dump((array) reset($xml->xpath("(//c)[3]")))

This also works with unset operations as outlined in an answer to:

  • Remove a child with a specific attribute, in SimpleXML for PHP
Thursday, August 18, 2022

Not sure why you used namespace notaion in the first place(the sample xml is not namespaced)

In your xpath, you need to select all condition/normal tags, not the condition tag as you were doing... Also, xpath() returns a list, so foreach over it. You don't need to access it as children, unless you want to parse the children of $child. There it would make sense, and it would work as expected.

foreach ($xml->xpath("/Condition/Normal") as $child) {
    echo $child["type"] . "="  . $child->getName()."<br/>";


Monday, September 12, 2022

You can probably do it with XPath or something, but SimpleXMLElement::xpath() returns an array that is easy to sort:

usort($xQuery, function ($a, $b) { return strcmp($a->Make, $b->Make); });
foreach ($xQuery as $results) {
    // …
Saturday, September 24, 2022

just make sure the value in the options tags matches the value in the val method.

Saturday, November 5, 2022

Essentially you cant, Numbers, Booleans and Strings are immutable

Monday, November 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :