PHP Classes

Parse XML not in a file

Recommend this page to a friend!

      SimpleXML for PHP4  >  All threads  >  Parse XML not in a file  >  (Un) Subscribe thread alerts  
Subject:Parse XML not in a file
Summary:How can I parse data not in an XML file?
Messages:2
Author:Tim Poulsen
Date:2009-02-18 17:30:09
Update:2009-02-19 16:27:58
 

  1. Parse XML not in a file   Reply   Report abuse  
Picture of Tim Poulsen Tim Poulsen - 2009-02-18 17:30:09
I'm trying to use your class to parse data I get back from a payment processor. I send them an XML stream and they send one back. Thus, the data I'm attempting to parse is not an XML file, nor does it begin with the usual <?xml version="1.0" encoding="ISO-8859-1?> line. I just get:

<response>
<element1></element1>
<element2></element2>
</response>

I tried modifying the xml_load_file method, replacing the file_get_contents() with a simple assignment of my XML data to the $data variable. I get a "Cannot open xml document:" error.

Any suggestions?

Thanks,
Tim


  2. Re: Parse XML not in a file   Reply   Report abuse  
Picture of Tim Poulsen Tim Poulsen - 2009-02-19 16:27:58 - In reply to message 1 from Tim Poulsen
Dumb error on my part, which I finally figured out.

-------------------------------------------------------------
/* **********
NEW FUNCTION IN simplexml.class.php BEFORE CLOSING BRACE
ESSENTIALLY A COPY OF xml_load_file
*************** */
function xml_load_data($file, $resulttype = 'object', $encoding = 'UTF-8')
{
$php_errormsg="";
$this->result="";
$this->evalCode="";
$values="";
if ($file=='') {
return 'Cannot open xml data stream: ' . (isset($php_errormsg) ? $php_errormsg : $file);
} else {
$data = $file;
}

$parser = xml_parser_create($encoding);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$ok = xml_parse_into_struct($parser, $data, $values);
if (!$ok) {
$errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",
xml_get_error_code($parser),
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser),
xml_get_current_column_number($parser),
xml_get_current_byte_index($parser));
}

xml_parser_free($parser);
if (!$ok)
return $errmsg;
if ($resulttype == 'array')
return $this->xml_reorganize($values);
// default $resulttype is 'object'
return $this->array2object($this->xml_reorganize($values));
}
-------------------------------------------------------------

Use like this:

$myxmldata = <<<EOT
<RESPONSE>
<AUTH_CODE>12345</AUTH_CODE>
<CLIENT_ID>1234567890</CLIENT_ID>
<COMMAND>SALE</COMMAND>
<PAYMENT_MEDIA>VISA</PAYMENT_MEDIA>
<PAYMENT_TYPE>CREDIT</PAYMENT_TYPE>
<REFERENCE>1234567890</REFERENCE>
<RESULT>CAPTURED</RESULT>
<RESULT_CODE>4</RESULT_CODE>
<TERMINATION_STATUS>SUCCESS</TERMINATION_STATUS>
<TRANS_AMOUNT>1.00</TRANS_AMOUNT>
<TRANS_DATE>2009.02.18</TRANS_DATE>
</RESPONSE>
EOT;

require_once "simplexml.class.php";
$sxml = new simplexml;
$data = $sxml->xml_load_data($myxmldata);
echo "<pre>";
print_r($data);
echo "</pre>";