Pergunta

XML Code (FileZilla Server.xml):

<FileZillaServer>
    <Users>
        <User Name="Bill">
            <Option Name="Pass">window</Option>
        </User>
        <User Name="Paul">
            <Option Name="Pass">apple</Option>
        </User>
    </Users>
</FileZillaServer>

PHP Code:

$filezilla = 'FileZilla Server.xml';
$xml = simplexml_load_file($filezilla);

foreach ($xml->Users->User as $user) {
    echo "<strong>".$user['Name']."</strong><br />";
        foreach ($xml->Users->User->Option as $option) {
            if ($option['Name'] == "Pass") {
                echo $option."<br />";
            }
        }
    echo "<br />";
}

Output is:

Bill
window

Paul
window

Output should be:

Bill
window

Paul
apple

It seems, that the inner foreach repeats itself without going further. How can I correct this?

Thanks.

Foi útil?

Solução

Since Option is child of User you can't be expecting $xml->Users->User->Option to start at the current user node.

You could reference Option for the current $user as follows:

foreach ($xml->Users->User as $user) {
    echo "<strong>".$user['Name']."</strong><br />";
        foreach ($user->Option as $option) {
            if ($option['Name'] == "Pass") {
                echo $option."<br />";
            }
        }
    echo "<br />";
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top