2

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... ;)

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 3
    Even though a dup, a good first post, provided all information needed and kept it minimal. – user3783243 Aug 27 '20 at 22:38
  • Could not really understand the added value of your post... more details from you would help in order I understand what I've missed. Tks – user2922434 Aug 28 '20 at 09:10
  • Before reading the re-route answer next to the title, I could find useful information here : https://stackoverflow.com/questions/3634599/using-xml-node-names-with-hyphens-in-php. – user2922434 Aug 28 '20 at 09:13

0 Answers0