-1

I have received this XML out from a curl request.

<?xml version="1.0" encoding="utf-8"?>
<transaction>
    <result>PENDING</result>
    <merchanttransid>343434343</merchanttransid>
    <transref>23232323</transref>
    <errorcode>000</errorcode>
    <errormessage/>
    <description/>
</transaction>
SMTP Error: Could not connect to SMTP host.

However, the server is replying with a additional error response of SMTP Error: Could not connect to SMTP host. Now, when parsing via simplexml_load_string().

It throw's an error:

Entity: line 10: parser error: Extra content at the end of the document (Error Number: 2)

Since, the XML is correct and just having an issue on the response, is there a way on how strip that extra line?

hakre
  • 193,403
  • 52
  • 435
  • 836
Louie Miranda
  • 1,071
  • 1
  • 20
  • 36
  • 1
    The best solution is to ask the creator of the service to fix it. – CodeZombie Feb 25 '13 at 08:26
  • The XML is not correct. This is also why SimpleXML does give error here. If you want to load it nevertheless, you need to use the sister-library *DOMDocument* with recover set to true. See [this answer in *"Fix malformed XML in PHP before processing using DOMDocument functions"*](http://stackoverflow.com/a/9281963/367456). – hakre May 10 '13 at 21:58

2 Answers2

0

You can try to call the function passing it parameters to ignore the error messages of reading XML:

$xml = simplexml_load_file($url, "SimpleXMLElement", LIBXML_NOERROR |  LIBXML_ERR_NONE);

You can see the full list with options which you can pass to this function: http://www.php.net/manual/en/libxml.constants.php

Another solution (it's not good if you want to parse big files) is to read characters line by line and then remove the last line.

$xml = '<?xml...?>';
$lines = explode("\n", $xml);
unset( $lines[count($lines)-1] ); // remove last line
$output = implode($lines); // output now contains xml without the last line

Hope that helps.

Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
0

If you know that the structure will be always like this, loop for every line, strip whitespaces from left and if the first character is different than "<" then remove the line. At the end give the result to SimpleXML.

Voitcus
  • 4,463
  • 4
  • 24
  • 40