dongyin6576 2017-12-14 09:46
浏览 23
已采纳

如何接收下述类型的数据并将其提取出来

I am trying insert the values that has been continuously provided by the data provider and store it into table . The below mentioned is the format:

GO XXX,YYY,ZZZ=Ss
            %<QUOTE symbol="XXX" name="testname" exchange="OTHER" basecode="5" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="322.70" id="combined"/><SESSION day="A" timestamp="20171211000000" last="59" id="previous"/></QUOTE>
            %<QUOTE symbol="YYY" name="testname2" exchange="OTHER" basecode="0" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212102000" open="169" high="169" low="161" last="169" previous="169" volume="60309" numtrades="6" pricevolume="1009.82" id="combined"/><SESSION day="A" timestamp="20171211000000" last="169" id="previous"/></QUOTE>
            %<QUOTE symbol="ZZZ" name="testname3" exchange="OTHER" basecode="9" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212105500" open="8" high="8" low="6" last="6" previous="8" volume="9799179" numtrades="22" pricevolume="7013.11" id="combined"/><SESSION day="A" timestamp="20171211000000" last="8" id="previous"/></QUOTE>

How to pass this data to controller function and extract each and every values such as 'symbol','name' etc...?

Please help me to solve this...

  • 写回答

2条回答 默认 最新

  • dongzhi7763 2017-12-14 09:55
    关注

    Using SimpleXML (after removing the % - assuming this is part of the data), you can easily get at the attributes...

    $data = <<< XML
    GO XXX,YYY,ZZZ=Ss
                %<QUOTE symbol="XXX" name="testname" exchange="OTHER" basecode="5" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="322.70" id="combined"/><SESSION day="A" timestamp="20171211000000" last="59" id="previous"/></QUOTE>
                %<QUOTE symbol="YYY" name="testname2" exchange="OTHER" basecode="0" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212102000" open="169" high="169" low="161" last="169" previous="169" volume="60309" numtrades="6" pricevolume="1009.82" id="combined"/><SESSION day="A" timestamp="20171211000000" last="169" id="previous"/></QUOTE>
                %<QUOTE symbol="ZZZ" name="testname3" exchange="OTHER" basecode="9" pointvalue="1.0" tickincrement="1" ddfexchange="k" lastupdate="20171212095542" mode="R"><SESSION day="B" timestamp="20171212105500" open="8" high="8" low="6" last="6" previous="8" volume="9799179" numtrades="22" pricevolume="7013.11" id="combined"/><SESSION day="A" timestamp="20171211000000" last="8" id="previous"/></QUOTE>
    
    XML;
    
    preg_match_all("/%<QUOTE(.*?)<\/QUOTE>/", $data, $list);
    foreach ( $list[0] as $item )  {
        $item = ltrim($item, "%");
        $xml = simplexml_load_string($item);
        echo (string)$xml['exchange'].PHP_EOL;
        foreach ( $xml->SESSION as $session )   {
            echo $session['day'].'-'.$session['timestamp'].PHP_EOL;
        }
    }
    

    The above code outputs...

    OTHER
    B-20171212104600
    A-20171211000000
    OTHER
    B-20171212102000
    A-20171211000000
    OTHER
    B-20171212105500
    A-20171211000000
    

    I've updated the code with your new data structure, I'm using regular expressions - which I'm not a huge fan of, but this should extract just the components you need. It uses the same code structure once it's extracted each individual item as before.

    To insert the data into a database, you need to look into something like PDO and how to add data to a database (anything like https://phpdelusions.net/pdo#dml will help).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大