Page 1 of 1

Can't access array in XMLMadeSimple2

Posted: Thu Apr 14, 2022 3:51 pm
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!

Re: Can't access array in XMLMadeSimple2

Posted: Fri Apr 15, 2022 11:09 am
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.

Re: Can't access array in XMLMadeSimple2

Posted: Sat May 07, 2022 12:02 pm
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