Page 1 of 2

XML MadeSimple [Default template] [Solved]

Posted: Mon Mar 08, 2010 9:11 am
by jo8e
Hello,

Could you please provide an example for the default template required to retrieve and display a XML data feed within CMSMS.

My XMl data is of the format;


 
 
 
 
 

Thank you in advance for any assistance,

Re: XML MadeSimple [Default template]

Posted: Tue Mar 09, 2010 11:26 am
by totophe
Dear Jo8e,

The best way to create templates with XML Made Simple is to explore step by step the process.

What you can first do is : {$xml|print_r} who'll output the full xml structure.

For example, let's take the url http://www.w3schools.com/XML/simple.xml and make a feed in XMLMS.

1. We do a {$xml|print_r} and we see that:

Code: Select all

            SimpleXMLElement Object
(
    [food] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Belgian Waffles
                    [price] => $5.95
                    [description] => two of our famous Belgian Waffles with plenty of real maple syrup
                    [calories] => 650
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => Strawberry Belgian Waffles
                    [price] => $7.95
                    [description] => light Belgian waffles covered with strawberries and whipped cream
                    [calories] => 900
                )

            [2] => SimpleXMLElement Object
                (
                    [name] => Berry-Berry Belgian Waffles
                    [price] => $8.95
                    [description] => light Belgian waffles covered with an assortment of fresh berries and whipped cream
                    [calories] => 900
                )

            [3] => SimpleXMLElement Object
                (
                    [name] => French Toast
                    [price] => $4.50
                    [description] => thick slices made from our homemade sourdough bread
                    [calories] => 600
                )

            [4] => SimpleXMLElement Object
                (
                    [name] => Homestyle Breakfast
                    [price] => $6.95
                    [description] => two eggs, bacon or sausage, toast, and our ever-popular hash browns
                    [calories] => 950
                )

        )

)
We see that there is multiple "food" elements.

2. We do a foreach on food and we output the name:

Code: Select all

{foreach from=$xml->food item=food}
<p>{$food->name}</p>
{/foreach}
It will give us:

Code: Select all

<p>Belgian Waffles</p>
<p>Strawberry Belgian Waffles</p>
<p>Berry-Berry Belgian Waffles</p>
<p>French Toast</p>
<p>Homestyle Breakfast</p>
3. You can now play with that with your own feeds :-)

Re: XML MadeSimple [Default template]

Posted: Tue Mar 09, 2010 12:00 pm
by janb
Hi ..and excuse me for jumping into this post..

I was fighting with this XML schema in XMLMS a while ago but gave it up due to other projects (module tests)
Any idea?

Code: Select all

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
	xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
	xmlns:rs='urn:schemas-microsoft-com:rowset'
	xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
	<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
		<s:AttributeType name='MemberId' rs:number='1' rs:maydefer='true' rs:writeunknown='true' rs:basetable='tblMember'
			 rs:basecolumn='MemberId' rs:keycolumn='true' rs:autoincrement='true'>
			<s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true'/>
		</s:AttributeType>
		<s:AttributeType name='Lastname' rs:number='2' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'
			 rs:basetable='tblMember' rs:basecolumn='Lastname'>
			<s:datatype dt:type='string' dt:maxLength='25'/>
		</s:AttributeType>
		<s:AttributeType name='Firstname' rs:number='3' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'
			 rs:basetable='tblMember' rs:basecolumn='Firstname'>
			<s:datatype dt:type='string' dt:maxLength='15'/>
		</s:AttributeType>
		<s:extends type='rs:rowbase'/>
	</s:ElementType>
</s:Schema>
<rs:data>
	<z:row MemberId='1' Lastname='Lastname' Firstname='Firstname'/>
</rs:data>
</xml>
JanB

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 12:48 am
by jo8e
Thank you for the example. I have been able to successfully get your example working on my site but I am unable to get any of the xml data from my feed to display.

The xml data source is slightly different, any help would be appreciated.




http://www.testurl
http://testurl/15286.jpg
test site
2010-03-15T12:16:24




I am trying to get the Listing info to display and have tried the following but does not seem to work,

{foreach from=$xml->Listings item=Listings}
{$Listing->Title}
{/foreach}

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 11:19 am
by janb
{foreach from=$xml->Listings->Listing item="Listings"}
  {$Listings->Title}

{/foreach}

JanB

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 3:42 pm
by totophe
Could you try this ?

Code: Select all

{foreach from=$xml->Listings item="Listings"}
   {$Listings->Title}<br />
{/foreach}
Or else to post here the result of this:

Code: Select all

{foreach from=$xml->Listings item="Listings"}
   {$Listings|var_dump}<br />
{/foreach}

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 4:34 pm
by janb
totophe ??

Code: Select all

{foreach from=$xml->Listings item="Listings"}
   {$Listings->Title}<br />
{/foreach}
won't work..

Code: Select all

{foreach from=$xml->Listings->Listing item="Listings"}
   {$Listings->Title}<br />
{/foreach}
works!

It is easier to understand looking at the output from {$xml|@print_r}
(hint: look at the page source)

Code: Select all

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [id] => 1234
            [nickname] => test
            [version] => 1.0.0
            [updateUrl] => http://www.testurl/
        )
 
    [Listings] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [generated] => 2010-03-14T12:14:27
                    [expires] => 2010-03-14T12:19:27
                )
 
            [Listing] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1234567
                        )
 
                    [Url] => http://www.testurl
                    [PhotoUrl] => http://testurl/15286.jpg
                    [Title] => test site
                    [EndDate] => 2010-03-15T12:16:24
                )
 
        )
 
)
JanB

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 9:26 pm
by totophe
Janb, yeah, I probably was tired when I made my reply :-)

Neverthenless, the second one is not logic. Listings should be a list of items.

Code: Select all

{foreach from=$xml->Listings item=listing}
 A listing object
{/foreach}
Otherwise, it make no sense...

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 9:27 pm
by totophe
JanB, for your other question, what does the var_dump gives you ?

Re: XML MadeSimple [Default template]

Posted: Sun Mar 14, 2010 10:38 pm
by janb
Hi totophe
Neverthenless, the second one is not logic. Listings should be a list of items.
jo8e gives us an xml with only one element at the Listing level.
I'm not sure what the purpose of his xml document is, but if there have been more than one element at the same level, the code is correct.

If there is only one element, the loop is not necessary and could be like this:

Code: Select all

{$xml->Listings->Listing->Title}
If we don't know how many attributes there is in a certain element we can write it like this:

Code: Select all

{foreach from=$xml->Listings->Listing key=KeySub item="ItemSub"}

               {foreach from=$ItemSub key=KeySubs item=ItemSubs} 
               {if $KeySubs}{$KeySubs}:{/if} {$ItemSubs}<br /> 
               {/foreach} 

{/foreach}
JanB, for your other question, what does the var_dump gives you ?
Should give us all elements and attributes / values in "Listings"...

Code: Select all

object(SimpleXMLElement)#151 (2) { ["@attributes"]=> array(2) { ["generated"]=> string(19) "2010-03-14T12:14:27" ["expires"]=> string(19) "2010-03-14T12:19:27" } ["Listing"]=> object(SimpleXMLElement)#152 (5) { ["@attributes"]=> array(1) { ["id"]=> string(7) "1234567" } ["Url"]=> string(18) "http://www.testurl" ["PhotoUrl"]=> string(24) "http://testurl/15286.jpg" ["Title"]=> string(9) "test site" ["EndDate"]=> string(19) "2010-03-15T12:16:24" } } 

JanB

Re: XML MadeSimple [Default template]

Posted: Mon Mar 15, 2010 7:46 am
by jo8e
:) Thank you very much JanB, the query works exactly as you have explained. Much appreciated.

Re: XML MadeSimple [Default template]

Posted: Mon Mar 15, 2010 7:58 am
by janb
Glad to help jo8e :)

Don't forget to add [Solved] to the subject..

JanB

Re: XML MadeSimple [Default template]

Posted: Wed Mar 17, 2010 2:42 pm
by totophe
janb wrote:
JanB, for your other question, what does the var_dump gives you ?
Should give us all elements and attributes / values in "Listings"...

Code: Select all

object(SimpleXMLElement)#151 (2) { ["@attributes"]=> array(2) { ["generated"]=> string(19) "2010-03-14T12:14:27" ["expires"]=> string(19) "2010-03-14T12:19:27" } ["Listing"]=> object(SimpleXMLElement)#152 (5) { ["@attributes"]=> array(1) { ["id"]=> string(7) "1234567" } ["Url"]=> string(18) "http://www.testurl" ["PhotoUrl"]=> string(24) "http://testurl/15286.jpg" ["Title"]=> string(9) "test site" ["EndDate"]=> string(19) "2010-03-15T12:16:24" } } 

JanB
And does it ? I didn't had time/opportunity to check that actually. As I return the SimpleXMLElement, if SimpleXML support it, you should have it.

Best regards,

Totophe

Re: XML MadeSimple [Default template] [Solved]

Posted: Wed Mar 17, 2010 2:47 pm
by janb
It does ...If you had time to look at the answer :)
Should give us all elements and attributes / values in "Listings"...

Code:
object(SimpleXMLElement)#151 (2) { ["@attributes"]=> array(2) { ["generated"]=> string(19) "2010-03-14T12:14:27" ["expires"]=> string(19) "2010-03-14T12:19:27" } ["Listing"]=> object(SimpleXMLElement)#152 (5) { ["@attributes"]=> array(1) { ["id"]=> string(7) "1234567" } ["Url"]=> string(18) "http://www.testurl" ["PhotoUrl"]=> string(24) "http://testurl/15286.jpg" ["Title"]=> string(9) "test site" ["EndDate"]=> string(19) "2010-03-15T12:16:24" } }
JanB

Re: XML MadeSimple [Default template] [Solved]

Posted: Thu Apr 15, 2010 9:09 am
by oaouh
Hi  :),

I am using the XML Made Simple module too.
I haven't any problem to display items, but I'd like to display only the latest 3. I used the parameter "max_items" into the tag but it doesn't work.
Do you have an idea of how to do ?

Thanks
Cécile