PHP 8.5.0 Alpha 4 available for testing

Voting

: four minus one?
(Example: nine)

The Note You're Voting On

xxxargonxxx at gmail dot com
8 years ago
How to view, edit and remove elements.
Speaking in terms of CRUD style, there is no methods to read, edit and remove nodes. But it is possible.

2. How child nodes works (understanding)
<?php
echo "Exercise 2 \n";

$rootNode = new SimpleXMLElement('<xml_root>complex node</xml_root>'); // Create root node
$childNode1 = $rootNode->addChild('type_one','node 1'); // Create child node 1 with name 'type_one'
$childNode2 = new SimpleXMLElement('<type_one>node 2</type_one>'); // Create child node 2 with name 'type_one'
$childNode3 = new SimpleXMLElement('<type_two>node 3</type_two>'); // Create child node 3 with name 'type_two' (different!)
// Now $rootNode has only one children because other child nodes was created separately
// Lets paste child node to root node
echo $childNode1;

$rootNode->{'type_one'} = $childNode2; // Note that the property name is 'type_one'
$rootNode->{'type_two'} = $childNode3; // Note that the property name is 'type_two'
var_dump($rootNode);
/*
object(SimpleXMLElement)#68 (2) {
["type_one"]=>
string(6) "node 2"
["type_two"]=>
string(6) "node 3"
}
*/
// We see that "node 1" disappeared, that's because assigning one node to some property of another in not a magic.
// To be clear, the real calls looks like:
$rootNode->{'type_one'}[0] = (string) $childNode2;
$rootNode->{'type_two'}[0] = (string) $childNode3;
// Explanation:
// A: (string)
// It means that you can't assign node itself, only its string representation because assigned node will be casted to string anyway.
// It also means that tag name of assigned node doesn't matter - its name will be whatever name you specified in {} braces.
// B: [0]
// The array access operator ([0]) means that each tag name (type_one and type_two) has its own collection with integer indexes.
// If you want to add another tag with same name, you should increment index:
$rootNode->{'type_one'}[1] = $childNode2;
var_dump($rootNode);
/*
object(SimpleXMLElement)#68 (2) {
["type_one"]=>
array(2) {
[0]=>
string(6) "node 2"
[1]=>
string(6) "node 2"
}
["type_two"]=>
string(6) "node 3"
}
*/

// Note 1. The value of $childNode1 has been changed to "node 1" - that's because the child node, returned from addChild() method
// is referenced with parent node. If you change its content in one side - it changes in other side too:
$childNode1->{0} = 'renewed node 1';
// But it won't happen with $childNode2 - it is single and has no reference with parent node:
$childNode2->{0} = 'renewed node 2';
var_dump($rootNode);
/*
object(SimpleXMLElement)#68 (2) {
["type_one"]=>
array(2) {
[0]=>
string(14) "renewed node 1"
[1]=>
string(6) "node 2"
}
["type_two"]=>
string(6) "node 3"
}
*/

// Note 2. When you added child nodes, the parent node's string content is still alive, var_dump doesn't show it,
// but you can see through echoing XML:
echo "--1--\n".$rootNode->asXML();
/*
<xml_root>complex node <--------- here!
<type_one>renewed node 1</type_one>
<type_two>node 3</type_two>
<type_one>node 2</type_one>
</xml_root>
*/
// See 'complex node'? Bad news that you can't change string content anymore, if we try to call `$rootNode->{0} = 'something'` -
// it overrides all content - both string and child nodes! I don't know how to write to string content only in that case.
// But still you can read string content and remove entire node as described above.

// So the following statements gives same result, except $result variable value:
$someNode = new SimpleXMLElement('<div>text</div>');
$result = $rootNode->addChild('div','text'); // $result is SimpleXMLElement
$result = $rootNode->addChild($someNode->getName(),$someNode); // $result is SimpleXMLElement
$result = $rootNode->{'div'}[] = 'text*'; // $result is string 'text*'
$result = $rootNode->{$someNode->getName()}[] = $someNode; // $result is string 'text'
?>

<< Back to user notes page

To Top