通过 id 帮助进行 XML 解析 [英] XML parse by id help
问题描述
XML 新手,不知道我在做什么.我需要一个很好解释的简单方法来做到这一点.
XML newbie, have no idea what I'm doing. I need a well explained, simple way to do this please.
从上一页我使用查询字符串来获取 id 示例:test.php?id=AT
From a previous page I use query strings to get an id example: test.php?id=AT
我需要显示他们选择的部门内的所有员工姓名.下面是我的 XML 文档的片段.
I need to display all the employee names within the department that they select. Below is a snipit of my XML document.
<SOT>
<DEPARTMENT name="Aviation Technology" id="AT">
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>John J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>dsgriggs@purdue.edu</EMAIL>
</EMPLOYEE>
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>Jill J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>dsgriggs@purdue.edu</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
<DEPARTMENT name="Mechanical Engineering Technology" id="MET">
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>John J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>dsgriggs@purdue.edu</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
</SOT>
例如,如果他们选择使用 GET 拉入 AT id 的链接,我想在页面上显示以下内容:
For instance, if they select the link that uses a GET to pull in the AT id, I want to display the following on the page:
John J. Doe
Jill J. Doe
John J. Doe
Jill J. Doe
同样,我不知道我在这里做什么,所以任何帮助将不胜感激.
Again, I have no idea what I'm doing here so anyhelp would be greatly appreciated.
我尝试了以下代码,但它只显示每个部门的名字.知道如何修改它以显示部门内的所有名称吗?
I tried the following code but it only displays the first name for every department. Any idea how to ammend it to show all the names within a department?
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","faculty1.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("DEPARTMENT");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
推荐答案
After some some hints from Gordon here's my edited code:
$xmlObject = smplexml_load_file("staff.xml");
$id = $_GET["id"];
$employees = null;
foreach($xmlObject as $key => $value){
$attributes = $xmlObject[0]->DEPARTMENT->attributes();
if($attributes["id"] == $id){
$employees = $xmlObject[0]->DEPARTMENT->EMPLOYEE;
break;
}
}
foreach($employees as $key => $value){
print $value->NAME . "\n";
}
基本上,您将在 $employees 变量中获得一个员工列表,您可以根据需要对其进行迭代并检索名称.
Basically you will get a list of employes in the $employees variable which you can iterate through and retrieve the name as you stated you wanted.
这篇关于通过 id 帮助进行 XML 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!