Can't access array in XMLMadeSimple2

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
reinhardmohr
Forum Members
Forum Members
Posts: 112
Joined: Sun Aug 06, 2006 2:36 pm
Location: Munich, Germany

Can't access array in XMLMadeSimple2

Post by reinhardmohr »

Hi,
I am not a programmer – so I have run into a problem and would be glad if someone could help me – thanks!

I describe the problem:
I need to include data from an xml file into my website to parse a table on a page.

So I installed XMLMadeSimple2 and loaded the xml file as a feed.
This printed neatly an xml tree with objects and arrays onto my page. Looks like this:
Bildschirmfoto 2022-04-14 um 16.58.26.png
Also when I add "debug“ to my page the console shows this:
Bildschirmfoto 2022-04-14 um 17.00.52.png
Now the XMLMadeSimple2 help tells me to access the data in an array like this:

Code: Select all

{$xml->team->@attributes->name}
I also tried this:

Code: Select all

{$xml->$team->@attributes->name}
But when I have a look at the page I always get a smarty error that says:

Code: Select all

Syntax error in template "content:content:content_en"  on line 3 "{$xml->team->@attributes->name}"  - Unexpected "@", expected one of: "{" , DOLLARID , "id, name" , "$"
In tried some variations like

Code: Select all

{$xml->team.attributes.name}
but always the error pops up. So the problem seems to be the array‘s name "@attributes" that starts with an @.
As I said: I am not a programmer. So I am lost with this problem.

Could someone help me? Thank you

Reinhard

P.S.: If there is an easier way of processing an xml file to access the data for my website – I‘d be more than glad to learn. Thank you!
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1922
Joined: Mon Jan 29, 2007 4:47 pm

Re: Can't access array in XMLMadeSimple2

Post by Jo Morg »

The way Smarty (and PHP for that matter) syntax works, variables, and object properties which are variables too, can only contain numbers, letters and underscores (see docs). You are trying to access an object, not an array, so Smarty parser will throw you an error when you try to access those that way. The workaround is to convert the object to an array:

Code: Select all

{$xml_team_array = (array)$xml->team}
And then access it as an array index:

Code: Select all

{$xml_team_array['@attributes']->name}
That's because array indexes can be of type string and strings can contain those special characters you have there. The initial problem you had comes from the way XMLMadeSimple2 creates the objects, possibly from arrays which should throw an error but for some reason the objects containing invalid variable names get created anyway, just are not accessible. I don't really now the module so I don't know if there are alternative ways to access the object properties (via a get method or something like that), but that workaround should be ok.
HTH.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
reinhardmohr
Forum Members
Forum Members
Posts: 112
Joined: Sun Aug 06, 2006 2:36 pm
Location: Munich, Germany

Re: Can't access array in XMLMadeSimple2

Post by reinhardmohr »

Hi, Jo Morg,

thanks for helping.
After having read your advice I researched the internet and somewhere found a solution that worked for me:
I can access the object / the array with the special character @ now by using this:

Code: Select all

$meetingAbbr->attributes()->teamHome
As I said: I am not a programmer; so I am not even really sure why it works – but it does work (I admit I do not really understand why I can't access something like "@attributes“ but when I spell it like "attributes()" I can access it …
But I hope someone who has the same problem can perhaps use this solution / this workaround.

Thanks for helping and for developing that great CMS!
Thanks

Reinhard
Post Reply

Return to “Modules/Add-Ons”