Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to read xml files in Silver Stripe


Reply


14 Posts   4579 Views

Avatar
Willr

Forum Moderator, 5513 Posts

28 February 2009 at 2:17pm

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.

Avatar
Jardiamj

Community Member, 17 Posts

28 February 2009 at 5:00pm

Edited: 28/02/2009 5:02pm

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.

Avatar
biapar

Forum Moderator, 435 Posts

31 August 2010 at 12:35am

How do you convert into DataObject?

Avatar
Willr

Forum Moderator, 5513 Posts

31 August 2010 at 9:18pm

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.

Avatar
Mo

Community Member, 509 Posts

1 September 2010 at 9:48am

Edited: 01/09/2010 9:48am

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

Avatar
biapar

Forum Moderator, 435 Posts

2 September 2010 at 4:09am

Edited: 02/09/2010 4:13am

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