正则表达式 获取xml中的数据 Record ItemId="1"Value20110524/Value /Item ItemId="7"Value13.82/Value /Item ItemId="8"Value13.94/Value /Item ItemId="9"Value13.79/Value /Item ItemId="11"Value13.85/Value /Item /Record RecordItemId="1"Value20110525
正则表达式 获取xml中的数据
$s =<<< XML<br /> <Record><br /> <Item Id="1"><Value>20110524</Value><br /> </Item><br /> <Item Id="7"><Value>13.82</Value><br /> </Item><br /> <Item Id="8"><Value>13.94</Value><br /> </Item><br /> <Item Id="9"><Value>13.79</Value><br /> </Item><br /> <Item Id="11"><Value>13.85</Value><br /> </Item><br /> </Record><br /> <Record><Item Id="1"><Value>20110525</Value><br /> </Item><br /> <Item Id="7"><Value>13.82</Value><br /> </Item><br /> <Item Id="8"><Value>13.86</Value><br /> </Item><br /> <Item Id="9"><Value>13.58</Value><br /> </Item><br /> <Item Id="11"><Value>13.60</Value><br /> </Item><br /> </Record><br /> XML;<br /> <br /> $x = simplexml_load_string("<root>$s</root>");<br /> foreach($x->Record as $node) {<br /> $t = array();<br /> foreach($node->Item as $v) $t[] = current($v->Value);<br /> print_r($t);<br /> }<br />
Array<br /> (<br /> [0] => 20110524<br /> [1] => 13.82<br /> [2] => 13.94<br /> [3] => 13.79<br /> [4] => 13.85<br /> )<br /> Array<br /> (<br /> [0] => 20110525<br /> [1] => 13.82<br /> [2] => 13.86<br /> [3] => 13.58<br /> [4] => 13.60<br /> )<br /> <br />
<br /> $count=preg_match_all('/<Record>(.+?)<\/Record>/is',$str,$m);<br /> echo $count;<br /> <br /> preg_match_all('/<Value>(.+?)<\/Value>/is',$str,$values);<br /> echo "<pre class="brush:php;toolbar:false">";<br /> print_r($values[1]);<br /> echo "<【本文来自鸿网互联 (http://www.68idc.cn)】/pre>";<br /> /*<br /> 2<br /> Array<br /> (<br /> [0] => 20110524<br /> [1] => 13.82<br /> [2] => 13.94<br /> [3] => 13.79<br /> [4] => 13.85<br /> [5] => 20110525<br /> [6] => 13.82<br /> [7] => 13.86<br /> [8] => 13.58<br /> [9] => 13.60<br /> )<br /> */<br />