Addicted Addicted - 2 months ago 19
PHP Question

Php cant reach XML element

I have this XML-File:

<hostgrouplist>
<recordcount>68</recordcount>
<hostgroup id="530">
<instance_id>1</instance_id>
<hostgroup_name>Track2_Web</hostgroup_name>
<members>
<host id="451">
<host_name>webeu1-2</host_name>
</host>
<host id="457">
<host_name>webeu2-2</host_name>
</host>
<host id="463">
<host_name>webus-2</host_name>
</host>
</members>
</hostgroup>
</hostgrouplist>


I want to list all host names of a hostgroup but I can't pass through the
<member>
tag.

Target is to do this in a loop, but it even didn't work without it.

The last thing I tried is this:

$xml = simplexml_load_file ('http://localhost/file.xml');
echo $xml->hostgroup[0]->members->host[0]->host_name;


Shouldn't this work? Do you have a solution? Nothing in the related topics helped me, I think it could be a problem with the id in the host tag

Answer

You mean like this:

$xml = simplexml_load_file('file.xml');

foreach ($xml->hostgroup[0]->members->children() as $host) {
    echo $host->host_name.'<br/>';
}

Outputs:

webeu1-2
webeu2-2
webus-2
Comments