Selecting XML sibling based on content of another sibling with PHP

I’m trying to learn XML and its interactions with PHP. I currently have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<websettings>
<application_settings>
   <application>
        <name>Home</name>
        <active>yes</active>
        <description>a description</description>
   </application>
   <application>
        <name>Application 2</name>
        <active>yes</active>
        <description>another description</description>
        <app_abbr>FTF</app_abbr>
   </application>
   <application>
        <name>Application 3</name>
        <active>yes</active>
        <description>A third description</description>
   </application>
</application_settings>

</websettings>

This is my PHP

$config_xml = simplexml_load_file($config_file);

//Active Applications
$CONFIG_APPLICATIONS = array();
foreach($config_xml->application_settings->application as $application){
   if($application->active == "yes") $CONFIG_APPLICATIONS[] = (string)$application->name;
}

//Homepage
$CONFIG_HOME_DESC = $config_xml->application_settings->application['name'=>'home']->description;
$CONFIG_APPLICATION2_DESC = $config_xml->application_settings->application['name'=>'Application 2']->description;

First I’m going to note that I just made up the syntax around application[‘name’=>‘home’] so that I can illustrate what I need, but I really have no idea what the syntax would be.
The goal is that I can select the description of one of the application nodes based on the applications name node. What would be the PHP/XML syntax to do this? I also want to be able to loop through the applications as seen in the first foreach loop. The foreach loop is working, now I just need to figure the second part out. Also, let me know if I need to change my XML, as i’m a bit of an XML beginner.

I’m aware I could do something like $config_xml->application_settings->application[3]->description. But to me this seems prone to breaking when I add more nodes.

Thanks for your help.

If you wanted to just get the description you can do array_pop($config->xpath(“//application[name=‘Home’]/description”));

i like that… i’ll have try XPath sometime…

Take a look at XPath. :slight_smile:


<?php
error_reporting(-1);
ini_set('display_errors', true);

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<websettings>
  <application_settings>
     <application>
          <name>Home</name>
          <active>yes</active>
          <description>a description</description>
     </application>
     <application>
          <name>Application 2</name>
          <active>yes</active>
          <description>another description</description>
          <app_abbr>FTF</app_abbr>
     </application>
     <application>
          <name>Application 3</name>
          <active>yes</active>
          <description>A third description</description>
     </application>
  </application_settings>
</websettings>';

$config = new SimpleXMLElement($xml);
var_dump(
  $config->xpath("//application[name='Home']")
);

/*
  array(1) {
    [0]=>
    object(SimpleXMLElement)#2 (3) {
      ["name"]=>
      string(4) "Home"
      ["active"]=>
      string(3) "yes"
      ["description"]=>
      string(13) "a description"
    }
  }
*/
?>