2

ok, this might be a stupid question, but how do I get one single element from an XML document?

I have this XML

$element = $response['linkedin'];

SimpleXMLElement Object
( 
    [id] => 575677478478
    [first-name] => John
    [last-name] => Doe
    [email-address] => john@doe.com
    [picture-url] => http://m3.licdn.com/mpr/mprx/123
    [headline] => Headline goes here
    [industry] => Internet
    [num-connections] => 71

I just want to assign first-name as $firstName

I can loop over it using xPath, but that just seems like overkill.

ex:

$fName = $element->xpath('first-name');
foreach ($fName as $name) 
{
    $firstName = $name;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • If that `XMLElement` is the the only thing you are working with in the `$response['linkedin']`, `$response[linkedin']->{'first-name'} = $name`; should be able to do it for you. – Jon Apr 19 '13 at 06:10
  • Thanks @Jon, that's what I was looking for! If you add this as an answer I'll accept. – Paul Dessert Apr 19 '13 at 06:27
  • 1
    Please also make use of the exisitng documentation which shows this and much more: http://php.net/simplexml.examples-basic – hakre Apr 19 '13 at 07:58

3 Answers3

3

If you access a list of (one or more) element nodes in SimpleXML as a single element, it will return the first element. That is by default (and outlined as well in the SimpleXML Basic Usage):

$first = $element->{'first-name'};

If there are more than one element, you can specify which one you mean by using the zero-based index of it, either in square (array-access) or curly (property-access) brackets:

$first = $element->{'first-name'}[0];
$first = $element->{'first-name'}{0};

This also allows you to create a so called SimpleXML self-reference to access the element itself, e.g. to remove it:

unset($first[0]);  # removes the element node from the document.

unset($first);     # unsets the variable $first

You might think your Xpath would be overkill. But it's not that expensive in SimpleXML. Sometimes the only way to access an element is with Xpath even. Therefore it might be useful for you to know that you can easily access the first element as well per an xpath. For example the parent element in SimpleXML:

list($parent) $element->xpath('..'); # PHP < 5.4

$parent = $element->xpath('..')[0];  # PHP >= 5.4

As you can see it is worth to actually understand how things work to make more use of SimpleXML. If you already know all from the SimpleXML Basic Usage page, you might want to learn a bit more with the

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • "_If you access a list of (one or more) element nodes in SimpleXML as a single element, it will return the first element_" - I am confused by this one. If I had an xml string with 2 parents and each parent had 5 children, why does `$xml->parent->count()` return 2 and `$xml->parent[0]->count()` return 5? Both $xml->parent and $xml->parent[0] seem to point to the same first parent element. – georaldc Aug 15 '18 at 19:39
  • Yeah it is confusing, to answer your question: That is b/c `$xml->parent->count()` counts the child-elements named *parent* of the `$xml` element, while `$xml->parent[0]->count()` counts all child elements of the first child element named *parent* from the `$xml` element. /// `$xml->parent` and `$xml->parent[0]` never point to the same object b/c the first is a list of child elements named *parent* and the second is the first child element named *parent*. – hakre Aug 19 '18 at 19:51
0

You can use XPath to find the first instance of a matching element.

/root/firstname[1] would give you the first instance of firstname in your document.

$res=$response['linkedin']->xpath('/first-name[1]');
Laurence Moroney
  • 1,263
  • 8
  • 20
0

Answer form per request. ^^

If that SimpleXMLElement is the only one contained within $resource['linkedin'], you can change it with:

$resource['linkedin']->{'first-name'} = $name;

That allows you direct access to the element without needing to do an xpath on it. ^^

Jon
  • 4,746
  • 2
  • 24
  • 37