I'm working with a fairly simple xml file (originally displayed as a table) and I need to read the values within each 'row' element (eg. the strings 'fail', '12345'...), but I don't know how to write the correct syntax. Here is my xml file ($ptpXml):
<?xml version="1.0" encoding="UTF-8"?>
<ResultList name='PTP'>
<row status='fail' id='12345' ang='0.0' pr='-18.5'/>/>
<row status='pass' id='54321' ang='0.0' pr='21.3'/>/>
</ResultList>
As of now, my code looks like this:
/** @var \SC\Core\SimpleXMLElement */
$ptpXml = simplexml_load_file($ptpXml, '\SC\Core\SimpleXMLElement');
foreach ($ptpXml as $idx => $item) {
echo $item->status . '<br>';
echo $item->id . '<br>';
echo $item->ang . '<br>';
echo $item->pr . '<br>';
}
I've tried multiple options such as:
$test = $item->row->status;
$test = $item->status;
$test = $ptpXml->status;
$test = $ptpXml->row->status;
But none of them worked. Sometimes it recongnizes $test as a xml object, but not as the string 'fail'. What am I doing wrong?
=============================================================== Edit: After a lot of testing and some reading, I managed to sort it out! It will look something like this: Code:
/** @var \SC\Core\SimpleXMLElement */
$ptpXml = simplexml_load_file($ptpXml, '\SC\Core\SimpleXMLElement');
...
foreach ($ptpXml as $key => $result) {
$rows = $result->attributes();
echo $rows->status . '<br>';
echo $rows->ang . '<br>';
...
}
With that the code loops through both rows and I am free to choose from my 'row' element whatever attributes I'd like to work with.
';` will also cause a problem - https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Nigel Ren May 11 '21 at 13:19