3060 Posts in 864 Topics by 646 members
| Go to End | Next > | |
| Author | Topic: | 1126 Views |
-
How to import XML data in a dataobject?

4 August 2011 at 8:05pm
How to import XML data in a dataobject?
-
Re: How to import XML data in a dataobject?

4 August 2011 at 9:05pm Last edited: 4 August 2011 9:05pm
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();
} -
Re: How to import XML data in a dataobject?

4 August 2011 at 9:42pm
And..
How Do I import If a node have more child?
-
Re: How to import XML data in a dataobject?

4 August 2011 at 9:53pm
if the $xml->Field is a list then do another foreach
-
Re: How to import XML data in a dataobject?

11 September 2011 at 11:06am
and what about images? I just have
public static $has_one = array( 'Photo' => 'Image' ); -
Re: How to import XML data in a dataobject?

11 September 2011 at 10:30pm
neglostyti,
Sorry I didn't understand what you really mean
Does it has something to do with XML ? -
Re: How to import XML data in a dataobject?

11 September 2011 at 10:33pm
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' ); ?
-
Re: How to import XML data in a dataobject?

12 September 2011 at 12:01am
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;
| 1126 Views | ||
| Go to Top | Next > |



