I have an object returned from $xml = simplexml_load_file($file). I'm trying to access 'location-id' and 'item-id', but I can't figure out how to access those pieces of info. If it only nested once, I know I could do something like $xml->{'item-id'} but it doesn't seem to work. What is the syntax for this? Sorry for the lack of formatting.. that's how it was returned on my browser.
SimpleXMLElement Object (
[item] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array (
[item-id] => AAA )
[locations] => SimpleXMLElement Object (
[location] => SimpleXMLElement Object (
[#attributes] => Array (
[location-id] => 111
)
[quantity] => 1
[pick-up-now-eligible] => false
)
)
)
[1] => SimpleXMLElement Object
[#attributes] => Array (
[item-id] => BBB
)
[locations] => SimpleXMLElement Object (
[location] => SimpleXMLElement Object (
[#attributes] => Array (
[location-id] => 111
)
[quantity] => 1
[pick-up-now-eligible] => false
)
)
)
)
)
Could somebody chime in? TIA!!
it'd be
$xml->item[0]->attributes('item-id');
$xml->item[0]->locations->location->attributes('item-id');
To access attributes in SimpleXML, array-style syntax can be used.
$element['attribute_name'];
The SimpleXMLElement::attributes() method is also available. The above would be:
$element->attributes()->attribute_name;
If the attribute is namespaced (e.g. blah:attribute_name) then you can provide that namespace (as the prefix, or URI) to the attributes() method.
$element->attributes('blah', TRUE)->attribute_name;
$element->attributes('http://example.org/blah')->attribute_name;
See the SimpleXML Basic Usage manual page for further information.
To put the above into practice for this individual question, to print the item-id attribute of the second <item> element, you could use:
echo $xml->item[1]['item-id'];
The example below loops over the <item> elements and prints their associated item-id and (the first) location-id values.
foreach ($xml->item as $item) {
$location = $item->locations->location;
echo 'Item ID: ' . $item['item-id'] . "\n";
echo 'Locations: ' . $location['location-id'] . "\n";
}
Related
I got problem getting value from xml data, here is my data
[a] => SimpleXMLElement Object
(
[b] => SimpleXMLElement Object
(
[id] => 123
[name] => Daughter
[a] => SimpleXMLElement Object
(
[b] => SimpleXMLElement Object
(
[id] => 234
[name] => Mom
[c] => 1
[a] => SimpleXMLElement Object
(
[b] => SimpleXMLElement Object
(
[id] => 345
[name] => Grandma
)
)
)
)
)
)
How I can get this data
123Daughter
234Mom
345Grandma
The sub child is lot not only 3 level, try to read this one XML File - Get specific child nodes in unlimited node depths but still can't understand, anyone can help me, thank you
You need to use the xpath function:
$xml = simplexml_load_string($xml);
foreach ($xml->xpath('//b') as $item) {
echo $item->id . $item->name . PHP_EOL;
}
or if you want print just name:
$xml = simplexml_load_string($xml);
foreach ($xml->xpath('//name') as $name) {
echo $name . PHP_EOL;
}
code snippet: https://3v4l.org/tYeBi#output
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>';
}
I need your help guys,
I have this issue:
When I use the bigbluebutton api for retrieve the meetings from the server I received this result:
Array
(
[returncode] => SimpleXMLElement Object ( [0] => SUCCESS )
[messageKey] => SimpleXMLElement Object ( )
[message] => SimpleXMLElement Object ( )
[0] => Array
(
[meetingId] => SimpleXMLElement Object ( [0] => 1111)
[meetingName] => SimpleXMLElement Object ( [0] => Test MeetingName )
[createTime] => SimpleXMLElement Object ( [0] => 1402148945933 )
[attendeePw] => SimpleXMLElement Object ( [0] => passw)
[moderatorPw] => SimpleXMLElement Object ( [0] => passw)
[hasBeenForciblyEnded] => SimpleXMLElement Object ( [0] => false )
[running] => SimpleXMLElement Object ( [0] => false )
)
)
Where the second array will be populated by the meetings. Now there is one meeting so u see only one element.
I don't need help with cycle for or foreach but I need your help for retrieve the meetingid or meetingname or attendepw.
i found this on stackoverflow :Get value from SimpleXMLElement Objectsimplexmlelement-object
and the solution was: $value = (string) $xml->code[0]->lat;
But I think this is not the solution for me.
I tried this code:
$array=$result['message'];
$id=$array['meetingId'];
But now I can't retrieved the id from this $id.
Sorry for my English, any help will be appreciated
To get the meetingId SimpleXMLElement Object
$meetingIdEl = $result[0]['meetingId'];
To get the value of meetingIdEl
$mettingId = $meetingIdEl->__toString(); // or (string)$meetingIdEl;
echo $meetingId; // 1111
SimpleXMLElement Documentation
I am having trouble understanding the proper syntax to print array results using the SimpleXMLElement. From my xml result i must ask the user to match themselves with one of the people found in the array, and am not sure what is the best way to do this.
Sample XML result:
[authentication] => SimpleXMLElement Object
(
[age] => SimpleXMLElement Object
(
[code] => 5
[ambiguous] => SimpleXMLElement Object
(
[person] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Paul Foreman
[question] => SimpleXMLElement Object
(
[id] => dcalc3
[prompt] => What+do+the+%3Cb%3Elast+four%3C%2Fb%3E+digits+of+your+Social+Security+Number+add+up+to%3F
[answer] => 5
)
)
[1] => SimpleXMLElement Object
(
[name] => Paul Foreman
[question] => SimpleXMLElement Object
(
[id] => dcalc3
[prompt] => What+do+the+%3Cb%3Elast+four%3C%2Fb%3E+digits+of+your+Social+Security+Number+add+up+to%3F
[answer] => 6
)
)
)
)
)
)
Solution im looking for:
<?php
$string = $xml_result;
$xml = new SimpleXMLElement($string);
$is_age_code = $xml->authentication->{'age'}->code;
if($x_is_age_code == '5'){
// code 5 means more than one match found
// Ask user verification question
// If answer matches question
// Set current user as that person
}
?>
How can i find out how many 'Persons' are in the array and identify them with a number?
To count the number of persons use:
echo count($xml->authentication->age->ambiguous->person);
To access the subnodes of a person use
echo $xml->authentication->age->ambiguous->person[0]->question->prompt;
or with a loop:
foreach ($xml->authentication->age->ambiguous->person as $person) {
echo $person->question->prompt;
}
I have a simpleXML output of:
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2
)
[currentTime] => 2013-02-05 21:26:09
[result] => SimpleXMLElement Object
(
[rowset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => characters
[key] => characterID
[columns] => name,characterID,corporationName,corporationID
)
[row] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Wrytha Cy
[characterID] => 209668693
[corporationName] => Deep Core Mining Inc.
[corporationID] => 1000006
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Eve Mae
[characterID] => 624980803
[corporationName] => Viziam
[corporationID] => 1000066
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Wrytha
[characterID] => 709227913
[corporationName] => The Flying Tigers
[corporationID] => 669350666
)
)
)
)
)
[cachedUntil] => 2013-02-05 21:35:04
)
I would like to loop through with my php loop and get "name' and "characterID". I've trying something like:
$simpleXML = simplexml_load_string($xml);
foreach ($simpleXML->result->rowset->row as $row) {
print_r($row);
$name = $row['#attributes']['name'];
echo $name.'<br>';
}
but $name is not being set. It's gonna be something simple, just not seeing it in my haste and first time with simpleXML.
Attributes are accessed using the syntax $element['attribute_name'], so in your case, you need $row['name'].
It's important to remember that SimpleXML objects are kind of magic - the $element->child, $element_list[0] and $element['foo'] syntax overloads the normal PHP logic to be useful. Similarly, (string)$element will give you the full textual content of an element, however it is broken up in the actual XML.
As such, the print_r output will not give you a "real" view of the object, so should be used with care. There are a couple of alternative debug functions I've written here which give a more accurate idea of how the object will behave.