Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to import XML data in a dataobject?

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

Page: 1 2
Go to End
Author Topic: 1696 Views
  • biapar
    Avatar
    Forum Moderator
    435 Posts

    How to import XML data in a dataobject? Link to this post

    How to import XML data in a dataobject?

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: How to import XML data in a dataobject? Link to this post

    how about...

    $mytxml = file_get_contents ('my.xml');
    $xml = simplexml_load_string($mytxml,null,LIBXML_NOCDATA);

    foreach ($xml->MyNode as $node) {
       $do = new MyDataObject();
       $do->Field = $node->Field;
       $do->write();
    }

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: How to import XML data in a dataobject? Link to this post

    And..

    How Do I import If a node have more child?

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: How to import XML data in a dataobject? Link to this post

    if the $xml->Field is a list then do another foreach

  • neglostyti
    Avatar
    Community Member
    5 Posts

    Re: How to import XML data in a dataobject? Link to this post

    and what about images? I just have    
    public static $has_one = array( 'Photo' => 'Image' );

  • kpower
    Avatar
    Community Member
    7 Posts

    Re: How to import XML data in a dataobject? Link to this post

    neglostyti,
    Sorry I didn't understand what you really mean Does it has something to do with XML ?

  • neglostyti
    Avatar
    Community Member
    5 Posts

    Re: How to import XML data in a dataobject? Link to this post

    let's pretend my xml structure is like:
    <item>
    <title>My title</title>
    <pathToPicture>/images/image1.jpg</pathToPicture>
    </item>

    how I can import images to MyDataObject if there is public static $has_one = array( 'Photo' => 'Image' ); ?

  • kpower
    Avatar
    Community Member
    7 Posts

    Re: How to import XML data in a dataobject? Link to this post

    First thing is getting the file path from your XML document. Please follow the instructions given above and use simplexml to retrieve the file path. Once the file path is gotten, you can create an object of Image that represents the actual image in the retrieved path. If you have a look at the documentation of File you can see that Image class contains the inherited setFilename() method, which can be used to make that Image object to represent the image in the given location.

    $img = new Image();
    $img->setFilename($pathFromXML);
    $img->Title = $titleFromXML;
    $imgID = $img->write();

    Then you can override the onBeforeWrite() method of your data object, obtain the image ID and add it to the has_one relation.

    $myDataObject->PhotoID = $imgID;

    1696 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.