1

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>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>

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>
  • Show us any piece of your code to help you – TOUDIdel Apr 27 '11 at 05:56
  • possible duplicate of [How to extract a node attribute from XML using PHP's DOM Parser](http://stackoverflow.com/questions/3993302/how-to-extract-a-node-attribute-from-xml-using-phps-dom-parser) – Gordon Apr 27 '11 at 06:49
  • possible duplicate of [getElementsByTagName with specific attribute](http://stackoverflow.com/questions/4224286/php-getelementsbytagname-with-specific-attribute/4224309#4224309) – Gordon Apr 27 '11 at 06:50
  • [Best methods to parse HTML/XML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Apr 27 '11 at 06:51
  • Replace the XPath query in [my answer](http://stackoverflow.com/questions/3993302/how-to-extract-a-node-attribute-from-xml-using-phps-dom-parser/3995983#3995983) to `/SOT/DEPARTMENT[@id="AT"]/EMPLOYEE/NAME` – Gordon Apr 27 '11 at 14:02
  • Sorry, this is a for class and I just don't have any idea what I'm doing. I've gotten it to throw out names finally. However, it just throws out the first name in every department. Could you point me in the right direction of how to form a loop so it will display all the names of a particular department? And yes, I have been looking at all kinds of examples and documentation and none of it really makes any sense to me. Below is the code I have: – Zach Rodimel Apr 27 '11 at 14:10
  • – Zach Rodimel Apr 27 '11 at 14:11
  • @Zach there is an [Edit link](http://stackoverflow.com/posts/5805163/edit) below the question for a reason. And when you update your question with that code above, dont forget to point out why it is JavaScript code when the question is tagged PHP. – Gordon Apr 27 '11 at 14:28
  • Thank you so much! That did it @Gordon. Sorry for being a pain. can I paypal you $5? I know it's not much but I'm a poor college student and it's what I got. Get me your email address and I'll buy you a coffee. Thanks again for the patience and help. I really appreciate it. – Zach Rodimel Apr 27 '11 at 14:30
  • @Zach keep your money please. SO is for free advice. Buy me a beer if we should ever meet instead ;) – Gordon Apr 27 '11 at 14:33
  • StackOverflow isn't a forum; if you need to add more details, please [edit your question](http://stackoverflow.com/posts/5799936/edit). Answers are supposed to be answers, not additional information about the question. –  Apr 27 '11 at 14:48
  • @ZachRodimel: Don't pay for help, please just ask good questions and be a good member of StackOverflow. –  Apr 28 '11 at 14:58

2 Answers2

1

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>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"
}
Homer6
  • 15,034
  • 11
  • 61
  • 81
1

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.

Community
  • 1
  • 1
rzetterberg
  • 10,146
  • 4
  • 44
  • 54
  • 1- for not explaining what I did wrong. Please familiarize with constructive feedback before teaching others the wrong things. – rzetterberg Apr 27 '11 at 14:41
  • I'm not trying to be an asshole, I'm just trying to prove a point here. If you tell me what I did wrong and or point me in the right direction I'm certainly going to understand what you mean and learn from it. It's a part of giving constructive feedback :) And like always, at the end of the day there are no wrong and rights when it comes to the way you solve a problem. – rzetterberg Apr 27 '11 at 14:49
  • 1
    fair enough. you dont need file_get_contents. you can use simplexml_load_file directly, but that wont be an array but a Composite of SimpleXmlElement instances. Also, you dont need get_object_vars to access the attributes but can simply do $value['id'] instead and then do $value->EMPLOYEE[0]->NAME to get the first employee or 1 to get the second or iterate over employees. See http://pastebin.com/BWfzNTKS for a full example. – Gordon Apr 27 '11 at 14:49
  • 1
    Yeah, sorry for not doing so right from the start. As an alternative, use an XPath to iterate over the desired elements directly. Now, if you somewhat amend your answer, I will happily revoke the downvote. – Gordon Apr 27 '11 at 14:56
  • 2
    No worries! I have read the documentation and learnt quite a bit, so thanks for pointing that out for me and ultimately making me a better programmer :) – rzetterberg Apr 27 '11 at 15:15