Simple PHP XML parser using SimpleXML
Say you have a simple XML file books.xml that looks like this:
<?xml version="1.0" encoding="UTF-8"?> <books> <book> <author> <name>Nikola</name> <surname>Brežnjak</surname> </author> <title>Some awesome book title</title> <year>2014</year> </book> <book> <author> <name>Patrick</name> <surname>Rothfuss</surname> </author> <title>Doors of stone</title> <year>20xx</year> </book> </books>
and you want to get all the names of the authors, you can use SimpleXML:
<?php $xml = simplexml_load_file("books.xml"); var_dump($xml); $data = array(); foreach ($xml as $book){ $data[] = (string)$book->author->name; } var_dump($data); ?>
Leave a Comment