0

I have this really simple xml that I receive via POST form a desktop app:

<?xml version="1.0" encoding="utf-8"?>
<root>
<receipt status="1" id="PAR/2" idreceipt="1" date="YYMMDD" errorstr="" />
<receipt status="1" id="PAR/2/2" idreceipt="2" date="YYYY-MM-DD HH:II:SS" errorstr="" />
<receipt status="0" id="PAR/2/3" idreceipt="3" date="YYYY-MM-DD HH:II:SS" errorstr="ERROR" />
</root>

I save it to a variable and then try to load it to array using simplexml_load_string($string). var_dump($xml) returns false. I know there are no contents, but when I try to print_r the attributes using foreach on every receipt it's empty too. Is the xml not well-formed or am I missing something in the PHP?

Whole PHP:

$string = $_POST['results'];
$xml = simplexml_load_string($string);
foreach ($xml->receipt as $value) {
print_r($value);
} 
kacper3w
  • 84
  • 1
  • 10

2 Answers2

0

There is a problem in parsing the XML.
I tried the following code (using some code from http://php.net/manual/en/function.libxml-get-errors.php):

<?php

libxml_use_internal_errors(true);

$string = $_POST['results'];

$loaded_xml = simplexml_load_string($string);
$xml = explode("\n", $string);

$errors = libxml_get_errors();

foreach ($errors as $error) {
    echo display_xml_error($error, $xml);
}

foreach ($loaded_xml->receipt as $value) {
  print_r($value);
}

function display_xml_error($error, $xml)
{
    $return  = $xml[$error->line - 1] . "\n";
    $return .= str_repeat('-', $error->column) . "^\n";

    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "Warning $error->code: ";
            break;
         case LIBXML_ERR_ERROR:
            $return .= "Error $error->code: ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "Fatal Error $error->code: ";
            break;
    }

    $return .= trim($error->message) .
               "\n  Line: $error->line" .
               "\n  Column: $error->column";

    if ($error->file) {
        $return .= "\n  File: $error->file";
    }

    return "$return\n\n--------------------------------------------\n\n";
}

And I got many errors. Here are the first two:

<?xml version=\"1.0\" encoding=\"utf-8\"?> <root> <receipt status=\"1\" id=\"PAR/2\" idreceipt=\"1\" date=\"YYMMDD\" errorstr=\"\" /> <receipt status=\"1\" id=\"PAR/2/2\" idreceipt=\"2\" date=\"YYYY-MM-DD HH:II:SS\" errorstr=\"\" /> <receipt status=\"0\" id=\"PAR/2/3\" idreceipt=\"3\" date=\"YYYY-MM-DD HH:II:SS\" errorstr=\"ERROR\" /> </root>
--------------^
Fatal Error 33: String not started expecting ' or "
  Line: 1
  Column: 14

--------------------------------------------

<?xml version=\"1.0\" encoding=\"utf-8\"?> <root> <receipt status=\"1\" id=\"PAR/2\" idreceipt=\"1\" date=\"YYMMDD\" errorstr=\"\" /> <receipt status=\"1\" id=\"PAR/2/2\" idreceipt=\"2\" date=\"YYYY-MM-DD HH:II:SS\" errorstr=\"\" /> <receipt status=\"0\" id=\"PAR/2/3\" idreceipt=\"3\" date=\"YYYY-MM-DD HH:II:SS\" errorstr=\"ERROR\" /> </root>
--------------^
Fatal Error 96: Malformed declaration expecting version
  Line: 1
  Column: 14

--------------------------------------------

Now, according to this post, This is probably caused by magic_quotes_runtime adding backslashes when you ... .

So, I think this will solve your problem:

<?php

libxml_use_internal_errors(true);

$string = $_POST['results'];

$string = stripslashes($string);

$loaded_xml = simplexml_load_string($string);
// rest of the code is the same as above
Community
  • 1
  • 1
Omid Kamangar
  • 5,768
  • 9
  • 40
  • 69
0

So I've tried pretty much everything - reinstalling the module, using the solution given by Ako, but it didn't work. It turned out my server while processing POST turned every special character into character entity and the parser didn't recognize string as a valid XML with &lt; and &gt; instead of actual < > so I added:

$string = html_entity_decode($string);

and it worked perfectly. Hope this helps somebody else!

kacper3w
  • 84
  • 1
  • 10
  • PHP has error logging capabilities. Enable error logging, for development enable all warnings and notices and you find out about problems in your code much faster. – hakre Aug 30 '14 at 16:11