child_node1 text-node
child_node2 text-node
childs_child_node text-node1
childs_child_node text-node2
EOS;
// create SimpleXml-Object with the string
$xmlObj = simplexml_load_string($xmlStr);
//~ var_dump($xmlObj); // dumps the structure
//
// Accessing the Text-Nodes
//
// The first and the second Text-Nodes
// could directly accessed by their Parent-Node-Names
echo $xmlObj->child_node1; // output: child_node1 text-node
echo $xmlObj->child_node2; // output: child_node2 text-node
// The third and fourth Text-Nodes
// could accessed through an array that exists for the
// childs_child_node's
echo $xmlObj->child_node3->childs_child_node[0]; // output: childs_child_node text-node1
echo $xmlObj->child_node3->childs_child_node[1]; // output: childs_child_node text-node2
//
// Accessing the Attribute-Nodes
//
// The Attributes-Hash/Array could accessed through the attributes-Method
// of the SimpleXmlElements
$attr = $xmlObj->child_node2->attributes();
echo $attr['testAttr']; // output: attrVal
//
// Changing Text-Nodes
//
$xmlObj->child_node1 = 'new child_node1 text-node';
//
// Get the modified xml
//
echo $xmlObj->asXml();
/*
output:
new child_node1 text-node
child_node2 text-node
childs_child_node text-node1
childs_child_node text-node2
*/
?>