I am receiving the following string from another server:
PHP Code:
$xml="<r_csp></r_csp><r_time></r_time><r_ref></r_ref><r_error></r_error><r_ordernum></r_ordernum><r_message>This is a test transaction and will not show up in the Reports</r_message><r_code></r_code><r_tdate>Wed Feb 8 11:14:59 2006</r_tdate><r_score></r_score><r_authresponse></r_authresponse><r_approved>APPROVED</r_approved><r_avs></r_avs>"
I want to be able to parse it out and get the following values put in to their respective variables:
If the XML is always that simple, you can use this:
PHP Code:
<?php
$xml="<r_csp></r_csp><r_time></r_time><r_ref></r_ref><r_error></r_error><r_ordernum></r_ordernum><r_message>This is a test transaction and will not show up in the Reports</r_message><r_code></r_code><r_tdate>Wed Feb 8 11:14:59 2006</r_tdate><r_score></r_score><r_authresponse></r_authresponse><r_approved>APPROVED</r_approved><r_avs></r_avs>";
if ($num_match > 0)
{
print_r($matches);
}
else
{
echo "no matches";
}
?>
$matches will contain an array of an array of values.
$matches[0] will contain the first <something></something> match, $matches[1] will contain the second <somethingelse></somethingelse> match, and so on...
Within each $matches[num] is an array. For example:
$matches[0][0] would contain entire matched string.
$matches[0][1] would contain just the inner tag ("something" if <something></something>).
$matches[0][2] would contain just the inner contents ("text" if <something>text</something>).
Now, if you XML ever gets a little more complicated, you can use this class that I created (actually, there are two... but one is used by the other):
PHP Code:
<?php
// --------------------------------------------
// | EP-Dev XML Class
// |
// | Copyright (c) 2002-2006 EP-Dev.com :
// | This program is distributed as free
// | software under the GNU General Public
// | License as published by the Free Software
// | Foundation. You may freely redistribute
// | and/or modify this program.
// |
// --------------------------------------------
/* ------------------------------------------------------------------ */
// XML Access Class
//
// Controls access to remote XML files. By doing this the script
// can easily access multiple XML files.
/* ------------------------------------------------------------------ */
class EP_Dev_XML
{
var $handle;
var $parser;
var $source;