Skip to content
Advertisement

How to parse xml data from this type of content ( from online URL)

I am trying to parse data from this url xml: https://o2v.nl/example2.xml and what I am trying to get is the [Property] information but I dont know what should be the next step to get [property] tag. What I have done so far is the code My code is:

<?php

 $source = 'https://o2v.nl/example2.xml';
 $xmlstr = file_get_contents($source);
 $xmlcont = new SimpleXMLElement($xmlstr);

 $xml = simplexml_load_string($xmlstr);
$xml_1 = $xml->Object;
print_r($xml_1);

?>

The output string is now as:

SimpleXMLElement Object ( [@attributes] => Array ( [Type] => System.Object[] ) [Property] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [Type] => System.Management.Automation.PSCustomObject ) [Property] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Address;Zipcode;City;Price;M2;Bdscode [Type] => System.String ) [0] => Hogeweg 6;2585 JD;Den Haag;4800000;402;AMS0001 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [Type] => System.Management.Automation.PSCustomObject ) [Property] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Address;Zipcode;City;Price;M2;Bdscode [Type] => System.String ) [0] => Prins Hendriklaan 15;1075 AX;Amsterdam;6750000;428;DH0003 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [Type] => System.Management.Automation.PSCustomObject ) [Property] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Address;Zipcode;City;Price;M2;Bdscode [Type] => System.String ) [0] => Ringvaartweg 235;3065 AD;Rotterdam;1195000;242;ROT0007 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [Type] => System.Management.Automation.PSCustomObject ) [Property] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Address;Zipcode;City;Price;M2;Bdscode [Type] => System.String ) [0] => Huizingalaan 187 189-191;3572 LL;Utrecht;3000000;1.155;UTR0009 ) ) ) )

So what should I do next to get all the [Property] tags? Thanks a lot.

Advertisement

Answer

If I understand you correctly, you are probably looking for something like this:

$results = $xmlcont->xpath('//Property/Property/text()');
foreach($results as $result) 
{
   echo $result , "n";
};

Output:

Hogeweg 6;2585 JD;Den Haag;4800000;402;AMS0001
Prins Hendriklaan 15;1075 AX;Amsterdam;6750000;428;DH0003
Ringvaartweg 235;3065 AD;Rotterdam;1195000;242;ROT0007
Huizingalaan 187 189-191;3572 LL;Utrecht;3000000;1.155;UTR0009
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement