Jump to:

23355 Posts in 17873 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » How to read xml files in Silver Stripe

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

Page: 1 2
Go to End
Author Topic: 4398 Views
  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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.

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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.

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    How do you convert into DataObject?

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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.

  • Mo
    Avatar
    Community Member
    504 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: How to read xml files in Silver Stripe Link to this post

    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

    4398 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.