duanhuang4306 2018-02-28 05:18
浏览 60
已采纳

如何使用php访问来自URL的xml输出中的数据点?

I am trying to access a single data point from the XML below. My goal would then be to insert into an html table. The following code has been giving me a stdClass Object and I can't figure out how to grab a single data point.

Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<response>
  <status>
    <code>0</code>
    <message>OK</message>
  </status>
  <data _idtype="ticker" _id="QYLD" _MstarId="F00000QEMN" _CurrencyId="CU$$$$$USD">
    <api _id="z87pq63qh3gbhraf">
      <TS-DayEndTradingVolume>55769</TS-DayEndTradingVolume>
      <TS-DayEndNAV>25.11</TS-DayEndNAV>
      <DP-NAVChange>0.11</DP-NAVChange>
      <DP-NAVChangePercentage>0.440000</DP-NAVChangePercentage>
      <FNA-FundNetAssets>186075814</FNA-FundNetAssets>
      <DP-DayEndDate>2018-02-27</DP-DayEndDate>
      <TS-MarketPriceOpen>25.2</TS-MarketPriceOpen>
      <PS-PriceToEarnings>0.03678</PS-PriceToEarnings>
      <TS-DayEndMarketPrice>25.06</TS-DayEndMarketPrice>
      <DMP-MarketPriceChange>-0.07</DMP-MarketPriceChange>
      <DMP-MarketPriceChangePercentage>-0.278552</DMP-MarketPriceChangePercentage>
      <DTTRV-NAV>24.98000</DTTRV-NAV>
      <DMP-SharesOutstanding>7448991.76741</DMP-SharesOutstanding>
      <DP-PremiumDiscount>0.32</DP-PremiumDiscount>
    </api>
  </data>
</response>

And the PHP:

$xml = simplexml_load_file($url); // URL with above XML

$jsonConvert = json_encode($xml);

$singleDataPoint = json_decode($jsonConvert);

print_r($singleDataPoint)

?>
  • 写回答

1条回答 默认 最新

  • duanjiong1952 2018-02-28 05:49
    关注

    Actually your json functions are not doing anything at all. json_encode is the opposite of json_decode, so you can just completely omit those two lines.

    The contents of the data points can be found, for example:

    $xml = simplexml_load_file($url);
    var_dump($xml->data->api->{'TS-DayEndTradingVolume'}->__tostring());
    

    If you're just echoing the value or using it in a string context, you don't need __toString() since that will be implied, but beware that without this, it is really an instance of an SimpleXMLElement object, not an actual string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)