Login +

How To ? / Using toXML() & pdoToXML()

If you want to easily format XML from model or recordsets, you can use SLS_FrontModel1::toXML() and SLS_FrontModel1::pdoToXML() methods.

Both methods can take an array of columns transformations or add you own keys formatted by others columns transformations.

Imagine this XML:

<View>
    <works>
        <work>
            <work_id><![CDATA[1]]></work_id>
            <work_title><![CDATA[fake title]]></work_title>
            <work_excerpt><![CDATA[<p>Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..</p><p>There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain</p>]]></work_excerpt>
            <work_content><![CDATA[<p>work content</p>]]></work_content>
            <work_date><![CDATA[2013-12-11 10:25:12]]></work_date>
            <work_file><![CDATA[work/file_56877809def.pdf]]></work_file>
            <work_photo><![CDATA[work/photo_2aef8732.jpg]]></work_photo>
            <skill_id><![CDATA[4]]></skill_id>
        </work>
        <work>
            <!-- ... -->
        </works>
    </works>
</View>
$works = $work->searchModels("work",array("skill"),array(),array(),array("work_position" => "desc"),array("start"=>"0","length"=>"10"));
$xml = $work->pdoToXML($xml,$works,array("work_title"    => "php:ucwords",
                                         "work_excerpt"  => array("php:strip_tags",
                                                                  "SLS_String:trimStringToLength:100"),
                                         "work_date"     => array("SLS_Date:getDate:MONTH_LITTERAL",
                                                                  "php:ucwords"),
                                         "work_file"     => "SLS_String:getUrlFile",
                                         "work_photo"    => "SLS_String:getUrlFileImg:_0",
                                         "work_url"      => array($this->_generic->getFullPath("Works","Work",array("Item"=>""),false),
                                                                  "work_title" => "SLS_String:stringToUrl:_",
                                                                  "-",
                                                                  "work_id",
                                                                  ".".$this->_generic->getSiteConfig("defaultExtension"))));

The rules are the following:

  • if the key exists in recordsets, apply transformation or array of transformations on this column
  • if the key doesn't exists in recordsets, concat transformations on others columns if exists, else directly the key name

The XML formated will be:

<View>
    <works>
        <work>
            <work_id><![CDATA[1]]></work_id>
            <work_title><![CDATA[Fake Title]]></work_title>
            <work_excerpt><![CDATA[Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit... There...]]></work_excerpt>
            <work_content><![CDATA[<p>work content</p>]]></work_content>
            <work_date><![CDATA[11 December 2013]]></work_date>
            <work_file><![CDATA[http://www.sillysmart.org/Public/Files/work/file_56877809def.pdf]]></work_file>
            <work_photo_original><![CDATA[work/photo_2aef8732.jpg]]></work_photo_original>
            <work_photo_0><![CDATA[http://www.sillysmart.org/Public/Files/work/photo_2aef8732_0.jpg]]></work_photo_0>
            <skill_id><![CDATA[4]]></skill_id>
            <work_url><![CDATA[http://www.sillysmart.org/Works/Work/Item/fake_title-1.sls]]></work_url>
        </work>
        <work>
            <!-- ... -->
        </works>
    </works>
</View>

Related :

References

  1. SLS_FrontModel - Mother class of models
  2. SLS_String - Tool SLS_String - String Treatment
  3. SLS_Date - Tool SLS_Date - Dates Handling