PHP SimpleXML rekursive Funktion zum auflisten von Kindern und Attribute

Brauche ich etwas Hilfe, auf die SimpleXML-Aufrufe für eine rekursive Funktion, die eine Liste der Elemente, die Namen und Attribute. Machen Sie eine XML-config-Datei-system, aber jedes script wird ein eigenes config-Datei sowie eine neue Namenskonvention. Also, was ich brauche, ist eine einfache Möglichkeit zum kartieren alle die Elemente, die Attribute haben, so wie in Beispiel 1 ich brauche eine einfache Möglichkeit zu nennen, alle Prozesse, aber ich weiß nicht, wie dies zu tun, ohne harte Codierung den Namen des elements wird die Funktion aufrufen. Gibt es eine Möglichkeit, rekursiv eine Funktion aufrufen, um einen child-element name? Ich habe die xpath-Funktionalität, aber ich sehe nicht, wie dies für Attribute.

Tut auch den XML-Code der Beispiele korrekt Aussehen? kann ich meine XML-Struktur wie diese?

Beispiel 1:

<application>
  <processes>
    <process id="123" name="run batch A" />
    <process id="122" name="run batch B" />
    <process id="129" name="run batch C" />
  </processes>
  <connections>
    <databases>
      <database usr="test" pss="test" hst="test" dbn="test" />
    </databases>
    <shells>
      <ssh usr="test" pss="test" hst="test-2" />
      <ssh usr="test" pss="test" hst="test-1" />
    </shells>
  </connections>
</application>

Beispiel 2:

<config>
  <queues>
    <queue id="1" name="test" />
    <queue id="2" name="production" />
    <queue id="3" name="error" />
  </queues>
</config>

Pseudo-code:

//Would return matching process id
getProcess($process_id) {
  return the process attributes as array that are in the XML
}

//Would return matching DBN (database name)
getDatabase($database_name) {
  return the database attributes as array that are in the XML
}

//Would return matching SSH Host
getSSHHost($ssh_host) {
  return the ssh attributes as array that are in the XML
}

//Would return matching SSH User
getSSHUser($ssh_user) {
  return the ssh attributes as array that are in the XML
}

//Would return matching Queue 
getQueue($queue_id) {
  return the queue attributes as array that are in the XML
}

EDIT:

Kann ich an zwei parms? auf die erste Methode, die Sie vorgeschlagen haben @Gordon

Ich habe es, thnx, siehe unten

public function findProcessById($id, $name)
{
    $attr = false;
    $el = $this->xml->xpath("//process[@id='$id'][@name='$name']"); //How do I also filter by the name?
    if($el && count($el) === 1) {
        $attr = (array) $el[0]->attributes();
        $attr = $attr['@attributes'];
    }
    return $attr;
}
Schreibe einen Kommentar