我试图将所有<name></name>
数据从下面的xml树中拉出来。我还在下面包含了我的php,这是行不通的。在此先感谢您的帮助
以下是XML的一部分,可以在url(它是一个很长的xml树)中找到:
<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>
这是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);
?>
答案 0 :(得分:1)
是的,T10H-Holdings
不能按原样使用。您可以使用大括号和引号进行换行。
然后要获取<Name>
的所有值,您可以使用foreach()
循环:
$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" ;
}
输出:
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
但是,如果你只想获得第一个,你可以使用:
echo $xml->data->api->{"T10H-Holdings"}->HoldingDetail->Name->__toString() ;
输出:
Apple Inc