0

I must loop an XML object and make a filter by a certain property. My code loops the entire file correctly, but I can't print any value due to a syntax issue. The hyphen in 'object-attribute' seems to not let me read the tag name. I get this error: Fatal error: Uncaught Error: Undefined constant "attribute" in C:\xampp\htdocs\php_program\looptest.php:3590 Stack trace: #0 {main} thrown in C:\xampp\htdocs\php_program\looptest.php on line 3590

I'm trying to use simplexml_load_string. My for each is down below the object. (this is an example, objects come with different values)

 <?php 

$xmlObject = 

'<?xml version="1.0" encoding="UTF-8"?>
<custom-objects xmlns="http://www.demandware.com/xml/lurex/customobject/2006-10-31">


 <custom-object type-id="sample" object-id="2398">
            <object-attribute attribute-id="akjscn">2.0</object-attribute>
            <object-attribute attribute-id="nvcb">N</object-attribute>
            <object-attribute attribute-id="masn">1111111111</object-attribute>
            <object-attribute attribute-id="12e">Y</object-attribute>
        </custom-object>
 <custom-object type-id="sample" object-id="2398">
            <object-attribute attribute-id="akjscn">2.0</object-attribute>
            <object-attribute attribute-id="nvcb">N</object-attribute>
            <object-attribute attribute-id="masn">1111111111</object-attribute>
            <object-attribute attribute-id="12e">Y</object-attribute>
        </custom-object>
 <custom-object type-id="sample" object-id="2398">
            <object-attribute attribute-id="akjscn">2.0</object-attribute>
            <object-attribute attribute-id="nvcb">N</object-attribute>
            <object-attribute attribute-id="masn">1111111111</object-attribute>
            <object-attribute attribute-id="12e">Y</object-attribute>
        </custom-object>
 <custom-object type-id="sample" object-id="2398">
            <object-attribute attribute-id="akjscn">2.0</object-attribute>
            <object-attribute attribute-id="nvcb">N</object-attribute>
            <object-attribute attribute-id="masn">1111111111</object-attribute>
            <object-attribute attribute-id="12e">Y</object-attribute>
        </custom-object>
 <custom-object type-id="sample" object-id="2398">
            <object-attribute attribute-id="akjscn">2.0</object-attribute>
            <object-attribute attribute-id="nvcb">N</object-attribute>
            <object-attribute attribute-id="masn">1111111111</object-attribute>
            <object-attribute attribute-id="12e">Y</object-attribute>
        </custom-object>

</custom-objects>';

$data = simplexml_load_string($xmlObject);
    foreach ($data->children() as $obj) {
        echo $obj->object-attribute. '<br/>';
        // echo "a-". PHP_EOL;
    }
?>
  • A hyphen character is not allowed in the standard syntax for object properties. You need to use an alternative syntax to access it. See here: https://stackoverflow.com/a/758458/1456201 – Jim Jan 21 '22 at 23:00
  • `object-attribute` means object MINUS attribute – Evert Jan 22 '22 at 00:45

0 Answers0