XML新手,不知道我在做什么。我需要一个很好解释,简单的方法来做到这一点。
在上一页中,我使用查询字符串来获取一个id示例:test.php?id = AT
我需要在他们选择的部门中显示所有员工姓名。下面是我的XML文档的片段。
<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的链接,我想在页面上显示以下内容:
John J. Doe
Jill J. Doe
同样,我不知道我在这里做什么,所以任何帮助都会非常感激。
我尝试了以下代码,但它只显示每个部门的名字。知道如何修改它以显示部门内的所有名称吗?
<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>
答案 0 :(得分:1)
您可以使用xpath表达式:
请参阅:https://github.com/homer6/altumo/blob/master/source/php/Xml/XmlElement.md
$xml = <<<XML
<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>
XML;
$xml_element = new \Altumo\Xml\XmlElement($xml);
$results = $xml_element->queryWithXpath( 'DEPARTMENT[@id="AT"]/EMPLOYEE/NAME' );
var_dump( $results );
array(2) {
[0]=>
string(11) "John J. Doe"
[1]=>
string(11) "Jill J. Doe"
}
答案 1 :(得分:1)
编辑:从Gordon获得一些提示后,这是我编辑过的代码:
$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变量中的雇员列表,您可以迭代并按照您的要求检索名称。