tags:

views:

36

answers:

1

I have some XML to work with, something like this:

<admin_list>
    <admin>
      <name>user1</name>
      <authentication_source>Local</authentication_source>
    </admin>
    <admin>
      <name>user2</name>
      <authentication_source>Local</authentication_source>
    </admin>
  </admin_list>

I can't seem to retrieve a specific admin though. The following:

  $admin = "user1";
  foreach ($subxml as $child) {
                 if ($child->admin->name == $admin) {
                  var_dump($child);
                  }
             }

returns an array including both user1 and user2. How can I go about outputting the XML of only the selected user. (in this case user1) So my desired output is:

 <admin>
      <name>user1</name>
      <authentication_source>Local</authentication_source>
 </admin>

Thank you!

+4  A: 

Using XPATH would probably be a good idea

$query = $simple_xml_resource->xpath('/admin_list/admin[name = "' . $name . '"]');

if(count($query) == 0)
{
    //no users found
}
else
{
    $user = $query[0];
    echo $user->asXML(); //outputs the XML
}
Tim Cooper
Doesn't seem to be working. Does [name require a closing bracket?
stormist
Yes, that would be it. Sorry about that.
Tim Cooper
Can you think of any other reason why the query would be returning null when the name does exist? Thanks for your input.
stormist
Found the problem. It was the preceding "/" on ->xpath('/admin_listwithout the / it worked great. Thanks!
stormist