I'm trying to parse out the following response:
SimpleXMLElement Object (
[responseType] => SimpleXMLElement Object (
[inputConceptName] => aspirin [inputKindName] => % )
[groupConcepts] => SimpleXMLElement Object (
[concept] => Array (
[0] => SimpleXMLElement Object (
[conceptName] => ASPIRIN
[conceptNui] => N0000145918
[conceptKind] => DRUG_KIND )
[1] => SimpleXMLElement Object ( #
[conceptName] => Aspirin
[conceptNui] => N0000006582
[conceptKind] => INGREDIENT_KIND )
) ) )
in PHP. I have it stored as a curl string:
$xml = simplexml_load_string($data);
print_r($xml);
How do I get just the conceptNui of the first object as a PHP variable?
Take a look at the documentation, it give simple example to understand the friendly syntax to extract the data :
http://www.php.net/manual/en/simplexml.examples-basic.php
In your case, it could be :
$xml->groupConcepts->concept[0]->conceptNui
As your structure is
SimpleXMLElement Object (
[responseType] => SimpleXMLElement Object (
[inputConceptName] => aspirin
[inputKindName] => % )
[groupConcepts] => SimpleXMLElement Object (
[concept] => Array ([0] => SimpleXMLElement Object (
[conceptName] => ASPIRIN
[conceptNui] => N0000145918
[conceptKind] => DRUG_KIND )
[1] => SimpleXMLElement Object (
[conceptName] => Aspirin
[conceptNui] => N0000006582
[conceptKind] => INGREDIENT_KIND )
)
)
)
$conceptNui = $mainObj->groupConcepts->concept[0]->conceptNui;
Related
I receive this warning when I try to get data from a call, I've tried with some solution for the same topic in stackoverflow but it doesn't work.
[2] simplexml_load_string(): Entity: line 4: parser error : Start tag expected, '<' not found
simplexml_load_string(): ^
the php code is as follow
$api = new MktApi();
$arr = array();
$xlm = $api->handleResponse($api->getBrands());
print_r($xlm);
$xml = simplexml_load_string($xlm);
and the print I have is like this
SimpleXMLElement Object ( [Head] => SimpleXMLElement Object ( [RequestId] => SimpleXMLElement Object ( ) [RequestAction] => GetBrands [ResponseType] => Brands [Timestamp] => 2017-05-04T16:29:44-0500 ) [Body] => SimpleXMLElement Object ( [Brands] => SimpleXMLElement Object ( [Brand] => Array ( [0] => SimpleXMLElement Object ( [BrandId] => 22912 [Name] => DC Comics [GlobalIdentifier] => 101020216193 ) [1] => SimpleXMLElement Object ( [BrandId] => 23324 [Name] => MIOS [GlobalIdentifier] => 101020216475 ) [2] => SimpleXMLElement Object ( [BrandId] => 32298 [Name] => 988 [GlobalIdentifier] => SimpleXMLElement Object ( ) ) [3] => SimpleXMLElement Object ( [BrandId] => 30015 [Name] => About Time [GlobalIdentifier] => SimpleXMLElement Object ( ) )
it's look like correct XML, I've tried to add the xml header but I still have the same issue.
It looks like $api->handleResponse(...) returns a SimpleXMLElement object. You don't need to parse it again.
I'm new to woking with XML with PHP. I have a fairly complex XML structure and am using simplexml in laravel and am having trouble accessing all the elements I need to get. I am able to loop through the large XML file but simpleXML is returning two objects per record and I only seem to be able to access the elements in 'header', the first object returned...
here is part of the xml object
SimpleXMLElement Object
(
[identifier] => RCM0635
[datestamp] => 2015-06-09
)
SimpleXMLElement Object
(
[lidoWrap] => SimpleXMLElement Object
(
[lido] => SimpleXMLElement Object
(
[lidoRecID] => RCM:1748
[descriptiveMetadata] => SimpleXMLElement Object
(
[objectClassificationWrap] => SimpleXMLElement Object
(
[objectWorkTypeWrap] => SimpleXMLElement Object
(
[objectWorkType] => SimpleXMLElement Object
(
[term] => musical instruments
)
)
[classificationWrap] => SimpleXMLElement Object
(
[classification] => Array
(
[0] => SimpleXMLElement Object
(
[term] => Cornet
)
[1] => SimpleXMLElement Object
(
[conceptID] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => SH_Class
)
)
)
)
)
)
With the code below I can get the elements in the header but I can't figure out how to get the other elements?
$streamer = \Prewk\XmlStringStreamer::createStringWalkerParser(public_path().'/xml/many_mimo_records.xml');
while ($node = $streamer->getNode()) {
$simpleXmlNode = simplexml_load_string($node);
echo (string)$simpleXmlNode->identifier;
echo (string)$simpleXmlNode->datestamp;
}
I'd be very grateful for any advice...
I'm not sure if I understand You but in major:
You act on while ($node = $streamer->getNode()) loop what means that after first iteration You'll get this object:
SimpleXMLElement Object
(
[identifier] => RCM0635
[datestamp] => 2015-06-09
)
so for the first time it's ok to read it like:
`
$simpleXmlNode = simplexml_load_string($node);
echo (string)$simpleXmlNode->identifier;
echo (string)$simpleXmlNode->datestamp;
`
but in the second iteration You have:
`
SimpleXMLElement Object
(
[lidoWrap] => SimpleXMLElement Object
(
[lido] => SimpleXMLElement Object
(
[lidoRecID] => RCM:1748
[descriptiveMetadata] => SimpleXMLElement Object
(
[objectClassificationWrap] => SimpleXMLElement Object
(
[objectWorkTypeWrap] => SimpleXMLElement Object
(
[objectWorkType] => SimpleXMLElement Object
(
[term] => musical instruments
)
)
[classificationWrap] => SimpleXMLElement Object
(
[classification] => Array
(
[0] => SimpleXMLElement Object
(
[term] => Cornet
)
[1] => SimpleXMLElement Object
(
[conceptID] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => SH_Class
)
)
)
)
)
)
`
so the code inside while is wrong.
i sugest to try something like this:
`
while ($node = $streamer->getNode()) {
$simpleXmlNode = simplexml_load_string($node);
if (!empty($simpleXmlNode->identifier))
echo (string)$simpleXmlNode->identifier;
if (!empty($simpleXmlNode->datestamp))
echo (string)$simpleXmlNode->datestamp;
if (!empty($simpleXmlNode->lidoWrap)) {
$lido = $simpleXmlNode->lidoWrap->lido;
echo (string)$lido->lidoRecID;
// and so on as the recursive XML node objects
}
}
`
When I set $sales like so $sales = $response->xml->transactions the contents look like:
SimpleXMLElement Object
(
[transaction] => Array
(
[0] => SimpleXMLElement Object
(
[transId] => 9999999999
[submitTimeUTC] => 2016-03-16T21:57:54Z
[submitTimeLocal] => 2016-03-16T14:57:54
[transactionStatus] => capturedPendingSettlement
[invoiceNumber] => 4b1008a87f5262f0c867
[firstName] => Foo
[lastName] => Bar
[accountType] => Visa
[accountNumber] => XXXX1414
[settleAmount] => 155.00
[marketType] => eCommerce
[product] => Card Not Present
)
[1] => SimpleXMLElement Object
(
[transId] =>
So ->transaction contains an array. But if I do this:
$sales = $response->xml->transactions->transaction;
foreach ($sales as $s) {
$list[]= $s->invoiceNumber;
}
$list contains
Array
(
[0] => SimpleXMLElement Object
(
[0] => ac502c094fe1722ba100
)
[1] => SimpleXMLElement Object
(
[0] => e2eb58351c87155e3720
)
[2] => SimpleXMLElement Object
(
[0] => 0bca2bb6d5a13e641b67
)
What am I doing wrong? Where are the SimpleXMLElement Objects coming from where I just expected the invoiceNumber string?
The reason you are getting the SimpleXMLElement type objects is because when you are doing $list[]= $s->invoiceNumber; you are adding the element as it is. Since it is part of the SimpleXMLElement object it has that type internally. This comes up a lot when working with SimpleXML so its good to keep in mind.
As mentioned in the comments casting to a string will give you just the value:
$list[] = (string) $s->invoiceNumber;
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.
I'm using the CloudFusion class to get Amazon.com data and my code is simple:
$items = $pas->item_search( "shoes", array(
"ResponseGroup" => "Small",
"SearchIndex" => "Blended" ));
$items = $items->body->Items;
echo "<pre>";
print_r( $items );
echo "</pre>";
This returns the following:
SimpleXMLElement Object (
[Request] => SimpleXMLElement Object
(
[IsValid] => True
[ItemSearchRequest] => SimpleXMLElement Object
(
[Keywords] => shoes
[ResponseGroup] => Small
[SearchIndex] => Blended
)
)
[TotalResults] => 737435
[TotalPages] => 245816
[SearchResultsMap] => SimpleXMLElement Object
(
[SearchIndex] => Array
(
[0] => SimpleXMLElement Object
(
[IndexName] => Kitchen
....
)
[Item] => Array
(
[0] => SimpleXMLElement Object
(
[ASIN] => B0001Z95QY
[DetailPageURL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0001Z95QY
[ItemLinks] => SimpleXMLElement Object
(
[ItemLink] => Array
(
[0] => SimpleXMLElement Object
(
[Description] => Technical Details
[URL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/tech-data/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0001Z95QY
) ....................
)
[1] => SimpleXMLElement Object
(
[ASIN] => B001ACNBZ8
[DetailPageURL] => http://www.amazon.com/Peet-Shoe-Dryer-Boot-Original/dp/B001ACNBZ8%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB001ACNBZ8
[ItemLinks] => SimpleXMLElement Object
(...................
)
)
What I'd like to do is get down to the "Item" level, then run a foreach to get each individual entry. I tried $items = $items->Item, but this returns only the first entry.
Any ideas?
First of all, you should avoid using print_r() on SimpleXMLElement, instead just take a look at the XML using asXML(). That's also what you should post here, instead of print_r()'s output.
I can't decipher the code you have posted so I'll take a wild guess and suggest that you try something like:
foreach ($items->body->Items->Item as $Item)
{
}
At any rate, if you want to iterate over something, foreach is the answer.