I need to parse an xml file received from a 3rd party in order to upload a section of it in mysql db. Using eclipse, php 7.3 and mysql 8.0 and a xml file versionned 1.0.
Here is an example of what the xml file could look like :
<?xml version="1.0" encoding="UTF-8"?>
<levels>
<level id="1" label-ref="Lab01">
<sublevels>
<sublevel id="0102020" category-ref="2" ></sublevel>
<sublevel id="0102022" category-ref="3" ></sublevel>
</sublevels>
</level>
</levels>
The issue is that label-ref
and category-ref
both contain a hyphen. Thi hyphen generates the following warning (future error) when used in my script:
Warning: Use of undefined constant ref - assumed 'ref'
I noticed that it's not the ref
word but the hyphen, -
, and I do not know how to bypass this. The xml is not under my control, I must use it as it is.
My php code :
$xmlOp //contains the above mentioned xml
foreach ($xmlOp->levels->level as $lev) {
$lev_id = $lev->attributes()->id;
$lev_label = $lev->attributes()->label-ref;
}
Thanks a lot for your help :) Cri
Update 28AUG : tks to Using XML node names with hyphens in PHP, just adding the {} helped... we are always smarter after we know the solution... ;)