-1

Looking to parse some XML data that is saved in a DB. In the DB its saved in raw XML:

<?xml version="1.0" encoding="utf-8"?>
  <RESPONSE>
    <SUCCESS>true</SUCCESS>
    <ERRORMESSAGE>
    </ERRORMESSAGE>
    <DATA>
    </DATA>
 </RESPONSE>

I want to be able to parse this data, and have found people normally using:

$xmlstr = <<<XML RAW XML XML;

But how can I use a php string in there?

So e.g.

$xmlstr = <<<XML $stringfromDB XML;

Of course that does not work, but how can it be done? Thanks.

Lovelock
  • 7,689
  • 19
  • 86
  • 186

1 Answers1

1

SimpleXML should do the trick for you. Check out this link, along with php manual basics.

$xmlData = /* query xml-data from DB as string */;
$xmlObj = simplexml_load_string($xmlData);
var_dump($xmlObj);

the <<<XML ...some code... XML; is just an alternative notation for declaring (multiline) strings, you don't have to do that, as long as you can obtain full XML document from DB as in your first snippet.

yergo
  • 4,761
  • 2
  • 19
  • 41