Jardiamj, if you are using SS2.3 it already has a method to do this. Convert::xml2array($xmlFile) which will return an array and it handles attributes as well.
Thanks again Willr, I tried to use it; but I don't know what I was doing wrong that it didn't work, I got tired of being trying to figure it out so I decided to build my own function, I don't need something so complex any way. I also modified my function so it handles attributes as well but I'm not using it now.
I will give a try to the method Convert::xml2array again. I think I know now what I was doing wrong, I'm new to everything related with web designing. I'm a bachelor in agriculture, but the web is a good tool and I want to use it. Silver Stripe has been very useful for me, in a short time I have a decent looking website and getting data from a external MySQL Database.
I have to say that Silver Stripe is great, for people like me with no so much knowledge in PHP programing or even web designing.
How do you convert into DataObject?
biapar - If your array has the Keys which match the dataobject field names then you can simply pass the array you get from Convert::xmlToArray() into the dataobject constructor. Like
$data = array('Field' => 'Value', 'SomeOtherField' => 'Hello');
$object = new MyDataObject($data);
echo $object->Field; // echos 'Value'
echo $object->SomeOtherField; // echos 'Hello'
As long as your xml keys match your dataobject fields you won't have to massage any of the data.
Never knew you can do that, you learn something new every day :).
I have done similar tasks before. I needed to import XML exported from Access and convert it into Blog posts. I found it was easiest to create a build task, and access it from:
Then i could just put all my logic in something isolated from the rest of the site. I suppose you could probably link that to a cron job, if you have SS 2.4?
Ok..I create function with xml2array and work, but this is the xml source
- <car id="1510834">
<version>1.2 16V TCE GT</version>
and this is array ( a part )
1.2 16V TCE GT
How use it as dataobject?
I've more car nodes and each car node has more attributes.