Search code examples
phpxmlsimplexml

Unable to Access @attributes in XML Node


I am trying to access the 'field' element in the 'criteria' node in the following XML:

<?xml version="1.0" encoding="utf-8"?>
<result>
    <product>
        <data>
            <field>spr_tech1</field>
            <value>S7</value>
            <criteria field="xfield_3">
                <criteria_list>Green</criteria_list>
                <criteria_list>Beige</criteria_list>
            </criteria>
        </data>
        <data>
            <field>spr_tech1</field>
            <value>S1</value>
            <criteria field="xfield_3">
                <criteria_list>Red</criteria_list>
                <criteria_list>Blue</criteria_list>
                <criteria_list>Yellow</criteria_list>
            </criteria>
        </data>
        <data>
            <field>spr_tech1</field>
            <value>S7</value>
            <criteria field="xfield_3">
                <criteria_list>Green</criteria_list>
            </criteria>
            <criteria field="tech_rt2">
                <criteria_list>Transistor</criteria_list>
            </criteria>
        </data>
    </product>
</result>

The code below results in the following error: Fatal error: Call to a member function attributes() on a non-object

function parseXmlFile($filename)
{
    $xml = file_get_contents($filename);
    $obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    $array = json_decode(json_encode($obj), true); // Convert to array}
    return $array;
}

$xform = parseXmlFile('transformations.xml');
foreach ($xform['product']['data'] as $data)
{
    echo (string)$data['field'] . '<br>';
    echo (string)$data['value'] . '<br>';

    foreach($data['criteria']->attributes() as $att => $val)
    { echo $att . ' = ' . $val . '<br>'; }
    echo $data['criteria']->attributes()->{'field'} . '<br>';

    foreach($data['criteria']['criteria_list'] as $att => $val)
    { echo $att . ' = ' . $val . '<br>'; }

    echo "-----------------------------------<br>";
}

print "<pre>";
print_r($xform);
print "</pre>";

I've tried a couple methods as you can see in the code, but neither are working for me.

Any help is appreciated!


Solution

  • It seems the problem is when you load the file. I tried it with file_get_contents() See below

    $xml_content = file_get_contents('path to your XML file');
    $xform = simplexml_load_string($xml_content);
    
    foreach ($xform['product']['data'] as $data)
    {
        echo (string)$data['field'] . '<br>';
        echo (string)$data['value'] . '<br>';
    
        foreach($data['criteria']->attributes() as $att => $val)
        { echo $att . ' = ' . $val . '<br>'; }
        echo $data['criteria']->attributes()->{'field'} . '<br>';
    
        foreach($data['criteria']['criteria_list'] as $att => $val)
        { echo $att . ' = ' . $val . '<br>'; }
    
        echo "-----------------------------------<br>";
    }
    
    print "<pre>";
    print_r($xform);
    print "</pre>";