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.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
- Previous 10 entries
- 1
- Page 22(current)
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:
http://www.mysite.com/dev/tasks/
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?
Cheers,
Mo
Ok..I create function with xml2array and work, but this is the xml source
- <car id="1510834">
- <model>
<infocar_code>074402200706</infocar_code>
<make>RENAULT</make>
<model>Twingo</model>
<version>1.2 16V TCE GT</version>
<body>2/3-Porte</body>
<fuel>Benzina</fuel>
<traction>anteriore</traction>
<kwatt>74</kwatt>
<cc>1149</cc>
<cylinders>4</cylinders>
<cvfiscali>14</cvfiscali>
<doors>3</doors>
<seats>4</seats>
<weight>980</weight>
<emission_class>E4</emission_class>
<emission_co2>138.0</emission_co2>
- <consumption>
<urban>7.6</urban>
<outer>4.9</outer>
<combined>5.9</combined>
</consumption>
</model>
....
and this is array ( a part )
car =
0 =
@attributes =
id =
1510834
model =
infocar_code =
074402200706
make =
RENAULT
model =
Twingo
version =
1.2 16V TCE GT
body =
2/3-Porte
fuel =
Benzina
traction =
anteriore
kwatt =
74
cc =
1149
cylinders =
4
cvfiscali =
14
doors =
3
seats =
4
weight =
980
emission_class =
E4
emission_co2 =
138.0
consumption =
urban =
7.6
outer =
4.9
combined =
5.9
exterior =
color =
Azzurro
paint =
metallizzato
interior =
color =
Nero
make =
Tessuto
start_date =
2010-06-23
usage =
....
How use it as dataobject?
I've more car nodes and each car node has more attributes.
Thanks
- Previous 10 entries
- 1
- Page 22(current)