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

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

问题描述:

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...

我正在尝试插入数据提供程序不断提供的值并将其存储到表中。 下面提到的格式为: p>

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

如何传递此信息 数据到控制器功能并提取每个值,如“符号”,“名称”等......? p>

请帮我解决这个问题...... p> DIV>

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).

Hi please take a look at the official PHP documentation for simpleXML: http://php.net/manual/en/ref.simplexml.php

I created a little snipped, which should get you started: https://eval.in/918963

$string = '<QUOTE symbol="XXX" name="name_xxx" exchange="testdata" basecode="C" pointvalue="1.0" tickincrement="1" ddfexchange="U" lastupdate="20171212095542" mode="R">
<SESSION day="B" timestamp="20171212104600" open="67" high="67" low="64" last="64" previous="59" volume="49000" numtrades="3" pricevolume="0000" id="combined"/>
<SESSION day="A" timestamp="20171211000000" last="59" id="previous"/>
</QUOTE>';

$xml = simplexml_load_string($string);

//Get all QUOTE attributes
print_r($xml->attributes());

//Get the first SESSION
print_r($xml->SESSION[0]);

//Get all attributes of SESSION[0]
print_r($xml->SESSION[0]->attributes());

//Get a specific attribute of SESSION[0]
print_r($xml->SESSION[0]->attributes()->day);