PHP: Search for a string in XML

后端 未结 2 1462
死守一世寂寞
死守一世寂寞 2021-01-24 01:57

I try the search code below but it is only showing the first child node. Is something missing from my code?

----catalog.xml----



        
相关标签:
2条回答
  • 2021-01-24 02:44

    I think this is what you want

    <?php
    $catalog = simplexml_load_file("catalog.xml");
    $category = $catalog->Category;
    $found = false;
    foreach($category as $c)
    {
        $menu = (string) ($c->Name);
        if ($menu == "CAT1" )
        {
            foreach ($c->Location as $loc)
            {
                echo $loc->Room;
            }
            $found = true; break;
        }
    }
    if (!$found)
    {
        echo "No result";
    }
    ?>
    

    Remove the break; statement if you can have more than one Category with the searched Name element.

    0 讨论(0)
  • 2021-01-24 02:46

    Try to revise your code to:

    echo $category->Location[$i]->Room;
    
    0 讨论(0)
提交回复
热议问题