note 82661 modified in ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:12:35 +0000 |
| Subject: | note 82661 modified in ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155493@lists.php.net to get a copy of this message | ||
I found simpleXML to be useful only in cases where the XML was extremely small, otherwise the server
would run out of memory (I suspect there is a memory leak or something?). So while searching for
alternative parsers, I decided to try a simpler approach. I don't know how this compares with
cpu usage, but I know it works with large XML structures. This is more a manual method, but it works
for me since I always know what structure of data I will be receiving.
Essentially I just preg_match() unique nodes to find the values I am looking for, or I
preg_match_all to find multiple nodes. This puts the results in an array and I can then process this
data as I please.
I was unhappy though, that preg_match_all() stores the data twice (requiring twice the memory), one
array for all the full pattern matches, and one array for all the sub pattern matches. You could
probably write your own function that overcame this. But for now this works for me, and I hope it
saves someone else some time as well.
// SAMPLE XML
<RETS ReplyCode="0" ReplyText="Operation Successful">
<COUNT Records="14" />
<DELIMITER value="09" />
<COLUMNS>PropertyID</COLUMNS>
<DATA>521897</DATA>
<DATA>677208</DATA>
<DATA>686037</DATA>
</RETS>
<?PHP
// SAMPLE FUNCTION
function parse_xml($xml) {
// GET DELIMITER (single instance)
$match_res = preg_match('/<DELIMITER value ?= ?"(.*)" ?\/>/', $xml,
$matches);
if(!empty($matches[1])) {
$results["delimiter"] = chr($matches[1]);
} else {
// DEFAULT DELIMITER
$results["delimiter"] = "\t";
}
unset($match_res, $matches);
// GET MULTIPLE DATA NODES (multiple instances)
$results["data_count"] = preg_match_all("/<DATA>(.*)<\/DATA>/",
$xml, $matches);
// GET MATCHES OF SUB PATTERN, DISCARD THE REST
$results["data"]=$matches[1];
unset($match_res, $matches);
// UNSET XML TO SAVE MEMORY (should unset outside the function as well)
unset($xml);
// RETURN RESULTS ARRAY
return $results;
}
?>
--was--
I found simpleXML to be useful only in cases where the XML was extremely small, otherwise the server
would run out of memory (I suspect there is a memory leak or something?). So while searching for
alternative parsers, I decided to try a simpler approach. I don't know how this compares with
cpu usage, but I know it works with large XML structures. This is more a manual method, but it works
for me since I always know what structure of data I will be receiving.
Essentially I just preg_match() unique nodes to find the values I am looking for, or I
preg_match_all to find multiple nodes. This puts the results in an array and I can then process this
data as I please.
I was unhappy though, that preg_match_all() stores the data twice (requiring twice the memory), one
array for all the full pattern matches, and one array for all the sub pattern matches. You could
probably write your own function that overcame this. But for now this works for me, and I hope it
saves someone else some time as well.
// SAMPLE XML
<RETS ReplyCode="0" ReplyText="Operation Successful">
<COUNT Records="14" />
<DELIMITER value="09" />
<COLUMNS>PropertyID</COLUMNS>
<DATA>521897</DATA>
<DATA>677208</DATA>
<DATA>686037</DATA>
</RETS>
<?PHP
// SAMPLE FUNCTION
function parse_xml($xml) {
// GET DELIMITER (single instance)
$match_res = preg_match('/<DELIMITER value ?= ?"(.*)" ?\/>/', $xml,
$matches);
if(!empty($matches[1])) {
$results["delimiter"] = chr($matches[1]);
} else {
// DEFAULT DELIMITER
$results["delimiter"] = "\t";
}
unset($match_res, $matches);
// GET MULTIPLE DATA NODES (multiple instances)
$results["data_count"] = preg_match_all("/<DATA>(.*)<\/DATA>/",
$xml, $matches);
// GET MATCHES OF SUB PATTERN, DISCARD THE REST
$results["data"]=$matches[1];
unset($match_res, $matches);
// UNSET XML TO SAVE MEMORY (should unset outside the function as well)
unset($xml);
// RETURN RESULTS ARRAY
return $results;
}
?>
http://php.net/manual/en/ref.simplexml.php