我解析一个大型 XML 文档,在解析子节点时遇到了很多麻烦。下面是我要解析的示例。
<link rel="http://xxxxx/people.employees" title="employees">
<people>
<link href="/154" rel="http://catalog/person" title="Guy Nom" />
<link href="/385" rel="http://catalog/person" title="Carrie Jin" />
<link href="/162" rel="http://catalog/person" title="Joe Zee" />
<link href="/2125" rel="http://catalog/person" title="Mark Polin" />
<link href="/9293" rel="http://catalog/person" title="Stephen Castor" />
<link href="/21822" rel="http://catalog/person" title="Callum Tinge" />
<link href="/2022" rel="http://catalog/person" title="Brian Lennon" />
<link href="/2040" rel="http://catalog/person" title="Jorja Fox" />
<link href="/2046" rel="http://catalog/person" title="Harry Harris" />
<link href="/2399" rel="http://catalog/person" title="Sam Muellerleile" />
</people>
</link>
<link rel="http://xxxxx/people/others" title="others">
<people>
<link href="/7143" rel="http://catalog/person" title="James Smith" />
</people>
</link>
我需要区分“员工”和“其他人”并将它们存储在单独的字段中。我想做如下的事情:
if($xmlReader->localName == 'link') {
if ($xmlReader->getAttribute('title') == "employees"){
//GO TO NEXT LINK TAG AND GET NAME
$myObject->employees[$myObject->employees_count]['name'] = $xmlReader->getAttribute('title');
$myObject->employees_count++;
} else if ($xmlReader->getAttribute('title') == "others"){
//GO TO NEXT LINK TAG AND GET NAME
$myObject->others[$myObject->others_count]['name'] = $xmlReader->getAttribute('title');
$myObject->others_count++;
}
}
显然,上面评论的部分对我来说是个问题。我不知道如何阅读这些子元素,而且在我看来,这方面的 PHP 文档一点也不好。我会很感激任何帮助。