Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

You can use example to read XML files.


XML file


<?xml version="1.0"?>
<property>
<type>Rent</type>
<price>917.00</price>
<no_of_room>10</no_of_room>
<ad_date>2018-10-17 6:59:59 pm</ad_date>
<images>
<image>http://abcabcabc/1.jpeg</image>
<image>http://abcabcabc/2.jpeg</image>
</images>
</property>

XML reader


declare(strict_types=1);

namespace AppBundle\Util\XMLParser;

use SimpleXMLElement;
use XMLReader;

class XMLParser
{
public function parse(string $filePath, string $rootNode): void
{
$XMLReader = new XMLReader();
$XMLReader->open($filePath);

while ($XMLReader->read()) {
if ($rootNode === $XMLReader->name && XMLReader::END_ELEMENT !== $XMLReader->nodeType) {
$property = new SimpleXMLElement($XMLReader->readOuterXML());

echo $property->asXML();
}
}

$XMLReader->close();
}
}

Usage


$parser = new XMLParser();
$parser->parse('property.xml', 'property');