从 XML 和 PHP 加载页面特定的子项

Load page specific children from XML & PHP

我有一个 xml 文件,其中根据 url 中定义的 ID 显示信息。例如id=vladivostokschool-parser.php?id=vladivostok,则显示信息:

Vladivostok University
Vladivostok
Russia
Russian

来自这个 xml 文件:

<schools>
    <school type="vladivostok">
        <name>Vladivostok University</name>
        <city>Vladivostok</city>
        <country>Russia</country>
        <language>Russian</language>
    </school>
    <school type="florianapolis">
        <name>Florianapolis University</name>
        <city>Florianapolis</city>
        <country>Brazil</country>
        <language>Portuguese</language>
    </school>
    <school type="gatineau">
        <name>Gatineau University</name>
        <city>Gatineau</city>
        <country>Canada</country>
        <language>French</language>
    </school>
</schools>

目前显示姓名、城市、国家和语言。我只想显示四个中的一两个,但不一定要显示全部四个。这是我的 php 代码:

$id = $_GET['id'];
$xml = simplexml_load_file('schools.xml');

foreach($xml->children() as $child) {  
   $role = $child->attributes();
   foreach($child as $key => $value) {           
       if($role == $id) {
            echo $value . "<br />";
       }            
   }
}

尝试这样的事情:

$string = '<schools>
    <school type="vladivostok">
        <name>Vladivostok University</name>
        <city>Vladivostok</city>
        <country>Russia</country>
        <language>Russian</language>
    </school>
    <school type="florianapolis">
        <name>Florianapolis University</name>
        <city>Florianapolis</city>
        <country>Brazil</country>
        <language>Portuguese</language>
    </school>
    <school type="gatineau">
        <name>Gatineau University</name>
        <city>Gatineau</city>
        <country>Canada</country>
        <language>French</language>
    </school>
</schools>';

//$id = $_GET['id'];
//$xml = simplexml_load_file('schools.xml');

$xml = new SimpleXMLElement($string);//test
$id = 'florianapolis';//test

foreach($xml->school as $key=>$data) { 
    if(strtolower($id) == strtolower($data['type'])){
        echo $key.' name:'.$data->name.' city:'.$data->city.' country:'.$data->country.' language:'.$data->language.'<br/>';
    }

}