Reputation: 31
XML newbie, have no idea what I'm doing. I need a well explained, simple way to do this please.
From a previous page I use query strings to get an id example: test.php?id=AT
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>[email protected]</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>[email protected]</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>[email protected]</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
</SOT>
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
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>
Upvotes: 1
Views: 610
Reputation: 10268
Edit: 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";
}
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.
Upvotes: 1
Reputation: 15159
You can use an xpath expression:
See: 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>[email protected]</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>[email protected]</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>[email protected]</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"
}
Upvotes: 1