Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first element of a SimpleXML object? [duplicate]

Tags:

php

simplexml

$xml is a SimpleXML object.

print_r($xml->Title);

outputs SimpleXMLElement Object ( [0] => K&H 3093 Extreme Weather Kitty Pad with Fleece Cover )

How do I access the first element?

print_r($xml->Title[0]);

outputs nothing!

like image 332
Edward Yu Avatar asked Oct 12 '13 00:10

Edward Yu


People also ask

What is SimpleXML extension?

SimpleXML is a PHP extension that allows users to easily manipulate/use XML data. It was introduced in PHP 5 as an object oriented approach to the XML DOM providing an object that can be processed with normal property selectors and array iterators.

What is SimpleXML in web technology?

SimpleXML is an extension that allows us to easily manipulate and get XML data. SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout.

Which of the following SimpleXML element method are used to add an attribute name with value to the element?

The SimpleXMLElement::attributes() function is an inbuilt function in PHP which is used to retrieve the attributes and its value from an XML tag in a SimpleXML object.


1 Answers

Try

echo (string) $xml->Title;

You have to cast it as a string.

like image 160
Galen Avatar answered Oct 11 '22 13:10

Galen