How to Get Values Of [#attributes] Element Using SimpleXML In PHP - php

I have problem accessing this attribue , $xml is my xml output using simpleXML .... here is the part of xml :
[component] => Array
(
[0] => SimpleXMLElement Object
(
[observationMedia] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => L30b39868-2c02-4f22-817f-3fc8ff059193
)
[text] => image description
[value] => SimpleXMLElement Object
(
[#attributes] => Array
(
[mediaType] => image/jpeg
)
[reference] => SimpleXMLElement Object
(
[#attributes] => Array
(
[value] => Label2.jpg
)
)
)
)
)
)
)
I am able to access the [text] element using this :
$xml->component->observationMedia->text
But Unable to access the attribute value , I tried this but didnot work :
$xml->component->observationMedia->value->reference->attributes()->value
If I use #attributes , then its gives error in php ...
Here is the xml data :
<component>
<observationMedia ID="L30b39868-2c02-4f22-817f-3fc8ff059193">
<text>image description</text>
<value mediaType="image/jpeg" xsi:type="ED">
<reference value="Label2.jpg" />
</value>
</observationMedia>
</component>

After looking at the xml data , I have solved my problem ..
Here is how I have got the attribute value :
echo (string) $component ->observationMedia ->value->reference['value']
Looking at the xml value has helped me ....

Related

PHP simplexml reading child tag attribute

I have a xml like below,
<y>
<n>
<n id='test1'></n>
<n id='test2'></n>
</n>
</y>
and want to read each "id" of child "n" tag .
I use this php code;
$xml = simplexml_load_file("my.xml");
echo $xml->n[0]->n;
but getting error,
Trying to get property of non-object
It should be : $xml->n->n[0] which is an array. If you print_r($xml) you might see like this:
SimpleXMLElement Object
(
[n] => SimpleXMLElement Object
(
[n] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => test1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => test2
)
)
)
)
)

How to display a value from a SimpleXML object (the array notation is confusing me)

I have a PHP file that uses cURL to retrieve some XML. I now want to retrieve a value from the XML but I cannot traverse to it as I am confused with the notation.
Here's my retrieved XML:
SimpleXMLElement Object
(
[#attributes] => Array
(
[uri] => /fruit/apple/xml/green/pipType
)
[result] => SimpleXMLElement Object
(
[JobOpenings] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 1
)
[FL] => Array
(
[0] => 308343000000092052
[1] => ZR_6_JOB
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[no] => 2
)
[FL] => Array
(
[0] => 308343000000091031
[1] => ZR_5_JOB
)
)
)
)
)
)
I have this XML stored in a variable called $xml using:
$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
Any help for how I can select the ZR_5_JOB element please?
I have tried countless times, the last effort I had was:
print_r($xml->result->JobOpenings->row[0]->FL[0]);
Could anybody please help?
(I know I will then need to do some iteration, but I'll deal with that later!)
First loop the JobOpenings rows to get each row separately and then you can access the childrens of that element in an easy way.
foreach($xml->result->JobOpenings->row as $item) {
echo $item->FL[0] . '<br>';
}

simplexml_load_string isn't creating attribute array if XML node is a value node

I am loading an XML string using simplexml_load_string and if the node doesn't have any child nodes of it's own then the attributes of that node don't seem to be mapped to the array correctly. Is there any way around this? Please see the CUSTOM_PROPERTY sections below:
Suppose I have the following XML:
<WEBSITE NAME="www.example.co.uk">
<CATEGORY ID="35702" NAME="CatName" FILE_NAME="" LONG_DESC="" SHORT_DESC="">
<CUSTOM>
<CUSTOM_PROPERTY NAME="CATTYPE_INDEX">6</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="TEMPLATE_ID">0</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="DISPLAY_LIMIT">10</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="HIDE_ON_MENU">0</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="CAT_COLOUR">#01b2a8</CUSTOM_PROPERTY>
</CUSTOM>
</CATEGORY>
When I use the following code:
$Xml = simplexml_load_string($Str);
print_r($Xml);
It returns this:
SimpleXMLElement Object
(
[#attributes] => Array
(
[NAME] => www.example.co.uk
)
[CATEGORY] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => 35702
[NAME] => CatName
[FILE_NAME] =>
[LONG_DESC] =>
[SHORT_DESC] =>
)
[CUSTOM] => SimpleXMLElement Object
(
[CUSTOM_PROPERTY] => Array
(
[0] => 6
[1] => 0
[2] => 10
[3] => 0
[4] => #01b2a8
)
)
)
)
)
CUSTOM_PROPERTY should have an attribute NAME mapped but it doesn't.
Your attributes are there but are not shown by print_r(). Be aware that print_r() is not a reliable way of viewing the entire SimpleXML structure.
Example:
Demo
$obj = simplexml_load_string($xml);
foreach($obj->CATEGORY->CUSTOM->CUSTOM_PROPERTY as $custom_property)
{
echo $custom_property->attributes()->NAME . "\n";
}
Outputs
CATTYPE_INDEX
TEMPLATE_ID
DISPLAY_LIMIT
HIDE_ON_MENU
CAT_COLOUR

SimpleXMLElement document - how to return value of children attributes in SimpleXMLElement Object?

Here is the XML:
<Routes>
<Route type="source">
<Table>
<Tablename>incoming</Tablename>
<Fields>
<Fieldsname ref="description" name="Route Name">description</Fieldsname>
<Fieldsname name="CID Match">cidnum</Fieldsname>
<Fieldsname name="DID Match">extension</Fieldsname>
<Fieldsname ref="dest">destination</Fieldsname>
</Fields>
</Table>
</Route>
</Routes>
Then my instantiation of it in PHP:
$doc = new SimpleXMLElement('routingConfig.xml', null, true);
print_r($doc->Route[0]) shows this:
SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => source
)
[comment] => SimpleXMLElement Object
(
)
[Table] => SimpleXMLElement Object
(
[Tablename] => incoming
[comment] => SimpleXMLElement Object
(
)
[Fields] => SimpleXMLElement Object
(
[Fieldsname] => Array
(
[0] => description
[1] => cidnum
[2] => extension
[3] => destination
)
[comment] => Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
)
)
)
)
)
Notice how the root value has the #attributes array. Why doesn't $doc->Routes[0]->Table->Fields->Fieldsname have #attributes? I realize I can get it via attributes(), but is there a way to make it be included in $doc?
EDIT
Apparently print_r() doesn't display every value in the array/object, exploring all children etc. Or perhaps SimpleXMLElement doesn't return it unless requested (seems like it should all be stored in $doc). If you do print_r($doc->Route[0]->Table->Fields->Fieldsname[0]);, it returns
SimpleXMLElement Object
(
[#attributes] => Array
(
[ref] => description
[name] => Route Name
)
[0] => description
)
Which shows the data I am looking for. But if I do a print_r($doc->Route[0]->Table->Field); the data does not appear.
The SimpleXMLElement object does some very advanced things in PHP. It implements a lot of the "magic" hooks that PHP provides so that it works in things like foreach() and is meant to be treated like a "black box". So because of that, using print_r() on it will give you misleading and incomplete information. You just can't rely on print_r() (or var_dump()) on a SimpleXMLElement object.
The way to debug the structure in a SimpleXMLElement is to simply look for the elements you're after: things like isset($xmlnode->child) work, for instance. So is_array($doc->Route[0]->Table->Fields->Fieldsname) will be true.

SimpleXML data missing

I have a XML which I parse with php simpleXML.
The XML:
<GetOneGetAll DateTimeSystem="28-06-2011 17:19:29" RetCode="200" RetVal="1" RetMsg="User ok.">
<User Id="bc5cb4cf-19a6-4504-8e1a-f72dd97bcc66" ReferedConfirmedUsers="0" TotalRecomendations="0" DistinctRecomendations="0">
<Name>Name</Name>
<Surname>Surname</Surname>
<Gender>F</Gender>
<Email>email#email.com</Email>
<RefererCode>59286904</RefererCode>
<CustomPhotoMessage HasCustomPhoto="0" HasCustomMessage="0"/>
<ReferedConfirmedUsersList/>
</User>
</GetOneGetAll>
When I print_r the var using simpleXML I get:
SimpleXMLElement Object
(
[#attributes] => Array
(
[DateTimeSystem] => 28-06-2011 17:22:52
[RetCode] => 200
[RetVal] => 1
[RetMsg] => Login ok.
)
[User] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Id] => bc5cb4cf-19a6-4504-8e1a-f72dd97bcc66
[ReferedConfirmedUsers] => 0
[TotalRecomendations] => 0
[DistinctRecomendations] => 0
)
[Name] => SimpleXMLElement Object
(
)
[Surname] => SimpleXMLElement Object
(
)
[Gender] => SimpleXMLElement Object
(
)
[Email] => SimpleXMLElement Object
(
)
[RefererCode] => SimpleXMLElement Object
(
)
[CustomPhotoMessage] => SimpleXMLElement Object
(
[#attributes] => Array
(
[HasCustomPhoto] => 0
[HasCustomMessage] => 0
)
)
[ReferedConfirmedUsersList] => SimpleXMLElement Object
(
)
)
)
Where is the data of Surname, Name, Email, Gender, etc.?
This is just a guess, but if you have xdebug installed, then the default recursion level of var_dump output is 3. This setting is xdebug.var_display_max_depth
You are using print_r, but some similar recursive limit could be being reached.

Categories