Fetching from XML using PHP

Good day to you! Perhaps I will ask a very stupid question, not accurate. It is necessary to parse the XML file, using the already built-in functions in the kernel, but I’m not catching up with something, I’m already nervous. On JS and Flash I know how, but in PHP it’s something else.
There is an XML file :

<?xml version="1.0" encoding="utf-8"?>
<setup_>
    <cat title="">
        <comp type="select" title=" ?">
            <conf title="" val="yes"></conf>
            <conf title="" val="no"></conf>
        </comp>
        <comp type="select" title=" ">
            <conf title="" val="auto"></conf>
            <conf title="  " val="with_allow"></conf>
        </comp>
    </cat>
</setup_>

Tell me, the full code, how can I get an array of <cat>tags into the <cat>variable, an array of tags into $comps<comp>and how to access their properties? P.S. Either I’m stupid or not, but I used Google, Yandex and other pranks, I didn’t find anything adequate, I’m in no hurry to see the code, so I would like to see the most complete code starting with <?phpand ending with ?>
thanks in advance…


Answer 1, authority 100%

Apparently, they searched really badly. There are even exhaustive examples in the official documentation.

$setup = new SimpleXMLElement($xml);
//    cat
foreach ($setup->cat as $cat) {
    //   
    echo $cat['title']."\n";
    //     comp 
    foreach ($cat->comp as $comp) {
        // ..
    }
}

Answer 2, authority 50%

I did this
The XML file had the following structure

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="AscColorl.xsd" generated="2011-09-27T09:13:12">
    <ColorsAll>
    <COLORID>531</COLORID>
    <COLOR>#005868</COLOR>
    <GAMMA>859</GAMMA>
    <DMC>-</DMC>
    <ANCHOR>-</ANCHOR>
    <MADEIRA>-</MADEIRA>
    </ColorsAll>
    ...
</dataroot>

declaring a variable

$str = file_get_contents("XMLS/AscColor/AscColorl.xml");
$xml = new SimpleXMLElement($str);

it was necessary to output in the form of a table,

<table width="100%" border="1" align="center" cellspacing="0" bordercolor="#666666" bgcolor="#F5F5F5" >
<tbody>
<?php
        foreach ($xml->ColorsAll as $ColorsAll)
    {
        ?>
        <tr>
        <td width="72"><p align="center"><?php echo "{$ColorsAll->COLORID}" ?></p></td>
        <td width="72"><p align="center"><?php echo "{$ColorsAll->GAMMA}" ?></p></td>
        <td width="72"><p align="center"><?php echo "{$ColorsAll->DMC}" ?></p></td>
        <td width="72"><p align="center"><?php echo "{$ColorsAll->ANCHOR}" ?></p></td>
        <td width="72"><p align="center"><?php echo "{$ColorsAll->MADEIRA}" ?></p></td>
        <td width="106" bgcolor=<?php echo "{$ColorsAll->COLOR}" ?>><p align="center"></p></td></tr><?php
    }
?>
</tbody>
</table>

that’s the whole example, if the principle is clear, you can try to solve your problem yourself


Answer 3, authority 50%

There is a very powerful XML search tool. Called DOMXPATH (see dock php https://www.php.net/ Manual / ru / class.domxpath.php ). You disassemble the XML content using DomDocument and further construct the request and select the desired nodes. Requests are built even using attribute values.