Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from an xml file

Tags:

php

xml

In a php script how to read and convert a XML document to an object and access the obtained object in order to get his data?

<?php
$xml ='<?xml version="1.0" encoding="UTF-8" ?>
<data request-id="ID">
<data name="Name1"
    d1="0"
    d2="0231234"
    d3="32584">
    <data name="Name2"
        d4="231234"
        d5="2012-06-06 18:18:10.000607"
        d6="3b048653-aaa9-485b-b0dd-d16e068230e9" />
    </data>
</data>';
$xml = simplexml_load_string($xml); 
//how to get the data d1? or d4? from the obtained object
?> 
like image 384
Amani Ben Azzouz Avatar asked Feb 13 '26 04:02

Amani Ben Azzouz


1 Answers

You can use this snippet:

<?php
$xmlstring = file_get_contents($filename);
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$object = json_decode($json);
like image 142
Galigin Avatar answered Feb 14 '26 20:02

Galigin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!