I am trying to pull all of the <name></name>
data into a table from the xml tree below. I have also included my php below that, which isn't work. Thanks in advance for the help
Here is part of the the XML, which can be found at the url (It's a bit of a long xml tree):
<response>
<status>
<code>0</code>
<message>OK</message>
</status>
<data _idtype="ticker" _id="QYLD" _MstarId="F00000QEMN"
_CurrencyId="CU$$$$$USD">
<api _id="oi397kxo7gb3o1c5">
<T10H-Holdings>
<HoldingDetail>
<HoldingType>E</HoldingType>
<Name>Apple Inc</Name>
<CountryId>USA</CountryId>
<Country>United States</Country>
<CurrencyId>USD</CurrencyId>
<Currency>US Dollar</Currency>
<CUSIP>037833100</CUSIP>
<ISIN>US0378331005</ISIN>
<Weighting>11.87795</Weighting>
<NumberOfShare>127166</NumberOfShare>
<MarketValue>23108606</MarketValue>
<ShareChange>0</ShareChange>
<SectorId>311</SectorId>
<Sector>Technology</Sector>
<GlobalSectorId>311</GlobalSectorId>
<GlobalSector>Technology</GlobalSector>
<Ticker>AAPL</Ticker>
<HoldingYTDReturn>3.78</HoldingYTDReturn>
</HoldingDetail>
<HoldingDetail>
<HoldingType>E</HoldingType>
<Name>Amazon.com Inc</Name>
<CountryId>USA</CountryId>
<Country>United States</Country>
<CurrencyId>USD</CurrencyId>
<Currency>US Dollar</Currency>
<CUSIP>023135106</CUSIP>
<ISIN>US0231351067</ISIN>
<Weighting>9.81789</Weighting>
<NumberOfShare>11950</NumberOfShare>
<MarketValue>19100760</MarketValue>
<ShareChange>0</ShareChange>
<SectorId>102</SectorId>
<Sector>Consumer Cyclical</Sector>
<GlobalSectorId>102</GlobalSectorId>
<GlobalSector>Consumer Cyclical</GlobalSector>
<Ticker>AMZN</Ticker>
<HoldingYTDReturn>27.70</HoldingYTDReturn>
</HoldingDetail>
<HoldingDetail>
<HoldingType>E</HoldingType>
<Name>Microsoft Corp</Name>
<CountryId>USA</CountryId>
<Country>United States</Country>
<CurrencyId>USD</CurrencyId>
<Currency>US Dollar</Currency>
<CUSIP>594918104</CUSIP>
<ISIN>US5949181045</ISIN>
<Weighting>9.50254</Weighting>
<NumberOfShare>191043</NumberOfShare>
<MarketValue>18487231</MarketValue>
<ShareChange>0</ShareChange>
<SectorId>311</SectorId>
<Sector>Technology</Sector>
<GlobalSectorId>311</GlobalSectorId>
<GlobalSector>Technology</GlobalSector>
<Ticker>MSFT</Ticker>
<HoldingYTDReturn>9.04</HoldingYTDReturn>
</HoldingDetail>
</T10H-Holdings>
</api>
</data>
</response>
Here is the PHP
<?php
$url =
"https://api.morningstar.com/v2/service/mf/oi397kxo7gb3o1c5/ticker/qyld?accesscode=ymjfk0fok83k7xg7kxse7tuzm7x0h3wp";
$xml = simplexml_load_file($url);
$data = ($xml->data->api->T10H-Holdings->HoldingDetail->{'Name'}-
>__tostring());
echo($data);
?>
Yes, T10H-Holdings
cannot be used as-is. You could wrap using curly brackets and quotes.
Then to get all values of <Name>
, you could use a foreach()
loop:
$url = "https://api.morningstar.com/v2/service/mf/oi397kxo7gb3o1c5/ticker/qyld?accesscode=ymjfk0fok83k7xg7kxse7tuzm7x0h3wp";
$xml = simplexml_load_file($url);
foreach ($xml->data->api->{"T10H-Holdings"}->HoldingDetail as $holding) {
echo (string)$holding->Name . "\n" ;
}
Outputs:
Apple Inc
Amazon.com Inc
Microsoft Corp
Facebook Inc A
Alphabet Inc C
NASDAQ-100 Mar. 16, 2018 Call 6825
Alphabet Inc A
Intel Corp
Cisco Systems Inc
Comcast Corp Class A
But, if you want to get only the first one, you could use:
echo $xml->data->api->{"T10H-Holdings"}->HoldingDetail->Name->__toString() ;
Outputs:
Apple Inc