Ankündigung

Einklappen
Keine Ankündigung bisher.

SimpleXML kein Zugriff auf letztes Attribut/ArrayElement

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • SimpleXML kein Zugriff auf letztes Attribut/ArrayElement

    Hallo,

    ich hab ein Problem mit SimpleXML und ich komm auf keinen grünen Zweig, ich wirklich so gut wie alles durchprobiert.

    Der XML Code:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <response> 
    <name>Test</name> 
    <methode>testing</methode> 
    <antwort> 
    <link url="http://example.com/"/> 
    <link url="http://test.de/" desc="Testen" alsText="yah" /> 
    <link url="http://php.de/"/> 
    </antwort> 
    <time used="12"/> 
    </response>

    Mein PHP Code:
    PHP-Code:
    $XMLContent '...xml von oben...';
    $XMLObject simplexml_load_string($XMLContent);
            
    $i=0;
            foreach(
    $XMLObject->antwort->link AS $b){
                
    $links[] = $XMLObject->antwort->link[$i]->attributes();
                
    $i++;
            }
    echo 
    "<pre>Links ".print_r($links,true)."</pre>"
    aber ich kann in den Array nicht das letzte Attribut/Element des SimpleXML Objects speichern.

    Es wird immer als SimpleXMLElement Object in den Array gespeichert.

    So stehts im Array:
    Code:
    Links Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [url] => http://example.com/
                    )
    
            )
    
        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [url] => http://test.de/
                        [desc] => Testen
                        [alsText] => yah
                    )
    
            )
    
        [2] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [url] => http://php.de/
                    )
    
            )
    )
    Ich will aber in dem Link-Array nur die URLs und als Index fortlaufende zahlen haben.

    So:
    Code:
    Links Array(
    [0]=>http://example.com
    [1]=>http://test.de
    [2]=>http://php.de
    ...
    )
    Ich bekomme es einfach nicht hin.

    Hoffe hier kann mir jemand weiterhelfen und erklären, an was ich nicht denke oder übersehe.

    Besten Dank für Hilfe!
    Gruß

  • #2
    Du selektierst ja alle Attribute, selektier eben nur das eine. Wie steht im Handbuch zu SimpleXML.

    Kommentar


    • #3
      Ja so:

      $links[] = $XMLObject->antwort->link[$i]->attributes()->url;

      Problem ist allerdings, dass es in der XML Datei (kommt von Schnittstelle) der Attributname url.from ist.

      Und: $links[] = $XMLObject->antwort->link[$i]->attributes()->url.from;

      mag PHP nicht.

      Gruß

      Kommentar


      • #4
        teste es mal so:
        PHP-Code:
            for ($i=0$icount($XMLObject->antwort->link); $i++) {
                foreach(
        $XMLObject->antwort->link[$i]->attributes() AS $a => $b){ 
                    if (
        $a == 'url') {
                        
        $links[] = $b;
                    }
                }
            } 

        Kommentar


        • #5
          Hat leider nicht den gewünschten Effekt siehe:

          Code:
          Links Array
          (
              [0] => SimpleXMLElement Object
                  (
                      [0] => http://example.com/
                  )
          
              [1] => SimpleXMLElement Object
                  (
                      [0] => http://test.de/
                  )
          .
          .
          .

          Ich versteh es einfach nicht...SimpleXML und dann doch nicht so simpel?

          Gruß

          Kommentar


          • #6
            Ich habe deine XML-Datei 1:1 übernommen, und hier ist mein Ergebnis:
            PHP-Code:
            Links Array
            (
                [
            0] => SimpleXMLElement Object
                    
            (
                        [
            0] => http://example.com/
                    
            )

                [
            1] => SimpleXMLElement Object
                    
            (
                        [
            0] => http://test.de/
                    
            )

                [
            2] => SimpleXMLElement Object
                    
            (
                        [
            0] => http://php.de/
                    
            )


            Kommentar


            • #7
              Ja, selbiges wie meines

              Ich möchte es aber so hinbekommen das kein SimpleXML Object im Array ist, also:

              PHP-Code:
              Links Array 

                  [
              0] => http://example.com/ 
                  
              [1] => http://test.de/
                  
              [2] => http://php.de/  

              Steht auch in meinem Startpost, da hab ich das ja auch schon so hinbekommen.
              Aber dieses Object will ich da raus haben.

              Und für das fehlt mir die Lösung, weil ich wirklich schon alles erdenkliche probiert hab.

              Gruß

              Kommentar


              • #8
                OK, sorry, da hatte ich dich nicht richtig verstanden.
                Dann sollte diese Zeile helfen:
                PHP-Code:
                $links[] = basename($b); 

                Kommentar


                • #9
                  Hm. Basename geht schon in die richtige Richtung und es macht aus den Domains/URLs komische Gebilde

                  Also wenn ich jetzt in der XML http://www.php.de/php-einsteiger/738...ayelement.html habe, dann zerschneidet die basename Funktion das bzw. nimmt den kompletten Anfang weg und lässt nur noch den Rest übrig, bei meinem Beispiel ab 73878-simplexml...

                  Also leider noch nicht das gewünschte Ergebnis.

                  Kann man das SimpleXML Object nicht irgendwie vernichten oder eine Methode die SimpleXML bietet darauf anwenden?

                  Gruß

                  Kommentar


                  • #10
                    Einfach in einen String casten sollte ausreichen.
                    http://www.php.net/manual/en/languag...es.typecasting

                    Kommentar


                    • #11
                      Versuch es mal folgendermaßen:
                      PHP-Code:
                      $XMLContent '...xml von oben...';
                      $XMLObject simplexml_load_string($XMLContent);
                      $i=0;
                      $links = array();
                      foreach(
                      $XMLObject->antwort->link as $value)
                      {
                          
                      $tmp $value->attributes();
                          foreach (
                      $tmp as $index => $value) {
                              if (
                      $index !== 'url') {
                                  continue;
                              }
                              
                      $links[] = trim((string) $value[0]);
                              break;
                          }   
                      }
                      echo 
                      "<pre>Links ".print_r($links,true)."</pre>"

                      Kommentar


                      • #12
                        Warum basename()? Das Attribut textContent sollte es sein. Magst du nicht erstmal ein XML/SimpleXML-Tutorial machen, bevor du jedes lösbare Problem hier bearbeiten lässt, das häufig auch nur für die gegooglet wird?

                        Kommentar

                        Lädt...
                        X