Friday, 14 December 2012

PHP - XML: Read from String

PHP XML Tips - Part 1: If you have a variable contains XML data, you can read/parse like this:
view source
print?
01    <?php
02    $xmlData =<<< END
03    <?xml version="1.0"?>
04    <datas>
05      <books>
06        <book>
07          <id>1</id>
08          <title>PHP Undercover</title>   
09          <author>Wiwit Siswoutomo</author>
10        </book>
11      </books>
12    </datas>
13    END;
14   
15    $xml = simplexml_load_string($xmlData)
16           or die("Error: Can not create object");
17   
18    ?>

Yes, we can use simplexml_load_string() function.

No comments:

Post a Comment