parent ( $node )

Gets the parent or a preferred ancestor of the current element.

Access

public

Parameters

Parameter

Type

Required

Description

$node

string

Optional

Name of the ancestor element to match and return.

Returns

Type

Description

CFSimpleXML

A CFSimpleXML object containing the requested node.

Examples

New additions in CFArray and CFSimpleXML.

A fairly contrived example showing how to use new additions in CFArray and CFSimpleXML.

This example uses anonymous functions, so it will only work as-is with PHP 5.3+. PHP 5.2.x users can refactor this example to use standard functions instead.

$ec2 = new AmazonEC2();
$response = $ec2->describe_images();

// Use an XPath query to find a matching set of nodes.
$list = $response->body->query('descendant-or-self::item[imageId[contains(., "aki-")] and architecture = "x86_64" and imageOwnerAlias = "amazon"]/imageLocation');

// CFArray::map() works similarly to array_map().
$results = $list->map(function($node)
{
	return $node->parent();
});

// Manually instantiate CFArray.
$image_locations = new CFArray(array());

/*
	For each entry in the $results array, pass it to the anonymous function.
	You can bind a variable (e.g., $image_locations) to the inner scope by
	using pass-by-reference.
*/
$results->each(function($node, $i, &$image_locations)
{
	$image_locations[] = (string) $node->imageLocation;

}, $image_locations);

// Display the first and last entries in the resulting array.
echo $image_locations->first() . PHP_EOL;
echo $image_locations->last() . PHP_EOL;
Result:
ec2-paid-ibm-images-ids/vmlinuz-2.6.16.60-0.30-xen.x86_64.manifest.xml
ec2-paid-ibm-images-db2/vmlinuz-2.6.16.60-0.30-xen.x86_64.manifest.xml

Source

Method defined in utilities/simplexml.class.php | Toggle source view (13 lines) | View on GitHub

public function parent($node = null)
{
    if ($node)
    {
        $parents = $this->xpath('ancestor-or-self::' . $node);
    }
    else
    {
        $parents = $this->xpath('parent::*');
    }

    return $parents[0];
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback