How to obtain data in xml using regular expressions

Source: Internet
Author: User
Use a regular expression to obtain the data token in xml.

Obtain the data 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

Obtain data in xml using a regular expression

20110524

13.82

13.94

13.79

13.85


20110525

13.82

13.86

13.58

13.60



This is part of the xml file. It is to extract the values following the value tag one by one from it. It is better to know several records.
It can be applied to a defined function, such
Function value ($ a, $ B, $ c, $ d, $ n ){
.......
}
For (I = 1; I <(number of records); I ++ ){
Value ($ a, $ B, $ c, $ d, $ I );
}
Cycles:
First Implementation of value (13.82, 13.94, 13.79, 13.85, 1)
Second Implementation value (13.82, 13.86, 13.58, 13.60, 2)
------ Solution ----------------------
Php provides a dedicated Method
$s =<<< XML

20110524

13.82

13.94

13.79

13.85


20110525

13.82

13.86

13.58

13.60


XML;

$x = simplexml_load_string(" $s ");
foreach($x->Record as $node) {
$t = array();
foreach($node->Item as $v) $t[] = current($v->Value);
print_r($t);
}
Array
(
[0] => 20110524
[1] => 13.82
[2] => 13.94
[3] => 13.79
[4] => 13.85
)
Array
(
[0] => 20110525
[1] => 13.82
[2] => 13.86
[3] => 13.58
[4] => 13.60
)


------ Solution ----------------------

$ Count = preg_match_all ('/ (. + ?) <\/Record>/is ', $ str, $ m );
Echo $ count;

Preg_match_all ('/ (. + ?) <\/Value>/is ', $ str, $ values );
Echo"
";
Print_r ($ values [1]);
Echo "<[this article comes from the Internet (http://www.68idc.cn)]/pre> ";
/*
2
Array
(
[0] = & gt; 20110524
[1] => 13.82.
[2] => 13.94.
[3] = & gt; 13.79
[4] = & gt; 13.85
[5] = & gt; 20110525
[6] = & gt; 13.82
[7] = & gt; 13.86
[8] = & gt; 13.58
[9] = & gt; 13.60
)
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.