Daten aus XML auf Webseite ausgeben

freak96

Ensign
Registriert
Aug. 2010
Beiträge
194
[FONT=&quot]Hallo zusammen,
mein Onkel hat jetzt ein IPad bekommen, kann aber nun den Benzinpreis der örtlichen Tankstelle nicht mehr nachschauen, da dieser in einer Flash-Animation :mad: ausgegeben wird. Die XML-Datei, in der die Preise stehen, sieht wie folgt aus:
[/FONT]
[FONT=&quot]
[/FONT]
Code:
<?xml version='1.0' encoding='utf-8'?> 
<spritpreise>
   <spritpreis sorte="sorte1" preis="1.40" /> 
   <spritpreis sorte="sorte2" preis="1.50" /> 
   <spritpreis sorte="sorte3" preis="1.60" /> 
</spritpreise>

[FONT=&quot]
[/FONT][FONT=&quot]Dumm ist nur das (Mobile)Safari auch keine reine[/FONT][FONT=&quot]n XML-Dokumente (wie Firefox es macht) ausgibt. Da ich aber leider gar keine Ahnung von PHP (oder wie kann man das sonst realisieren?) habe wollte ich nun mal hier nachfragen. Über Google habe ich schon diverse Tutorials (z.B. hier) gelesen, jedoch haben die XMLs dort alle eine andere Struktur.[/FONT]
[FONT=&quot]Bisher hab ich folgendes versucht:[/FONT]
[FONT=&quot]
[/FONT]
PHP:
<?php
$spritpreise = simplexml_load_file("http://www.tankstelle.de/preise.xml");
var_dump($spritpreise);
//echo$spritpreise->sorte[sorte1]->preis;
?>


Ich bekomme nun in einem Wirr-Warr die Preise auch angezeigt, jedoch will ich diese Ansicht meinem Onkel nicht zumuten. Es würde eigentlich reichen, wenn dort Super =>> 1.50 € angezeigt wird.
Mit echo$spritpreise->sorte[sorte1]->preis; (wie in Anleitung) bekomme ich aber leider immer einen Fehler. Danke schonmal für eure Antworten
 
Mit "sorte1" bekomme ich immer diesen hier:
Code:
[B]Notice[/B]:  Trying to get property of non-object in [B]C:\xampp\htdocs\test2.php[/B] on line [B]4[/B]
und das Wirr-Warr sieht so aus:

object(SimpleXMLElement)#1 (1) { ["spritpreis"]=> array(3) { [0]=> object(SimpleXMLElement)#2 (1) { ["@attributes"]=> array(2) { ["sorte"]=> string(6) "sorte1" ["preis"]=> string(4) "1.40" } } [1]=> object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(2) { ["sorte"]=> string(6) "sorte2" ["preis"]=> string(4) "1.50" } } [2]=> object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(2) { ["sorte"]=> string(6) "sorte3" ["preis"]=> string(4) "1.60" } } } }
 
Zuletzt bearbeitet:
PHP:
<?php
$spritpreise = simplexml_load_file("http://www.tankstelle.de/preise.xml");

echo '1 - '.$spritpreise->spritpreis[0]['sorte'].' , ';
echo $spritpreise->spritpreis[0]['preis'].' -';
echo "\n";

echo '2 - '.$spritpreise->spritpreis[1]['sorte'].' , ';
echo $spritpreise->spritpreis[1]['preis'].' -';
echo "\n";

echo '3 - '.$spritpreise->spritpreis[2]['sorte'].' , ';
echo $spritpreise->spritpreis[2]['preis'].' -';
echo "\n";
?>

edit:

In PHP arbeite ich persönlich lieber mit Arrays anstatt Objekten. Ich glaub den Code hab ich von php.org:
PHP:
function GetXMLTree( $xmldata )
{
  // we want to know if an error occurs
  $tmp = ini_get('track_errors');
  ini_set ('track_errors', '1');

  $attributes = array();

  $xmlreaderror = false;

  $result = '';

  $parser = xml_parser_create ('UTF-8');
  xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
  xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
  if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
    $xmlreaderror = true;
    echo 'xml read error - <pre>'.$xmldata.'</pre>';
  }
  xml_parser_free ($parser);

  if (!$xmlreaderror) {
    $result = array ();
    $i = 0;
    if (isset ($vals [$i]['attributes']))
      foreach (array_keys ($vals [$i]['attributes']) as $attkey)
      $attributes [$attkey] = $vals [$i]['attributes'][$attkey];

    $result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, 'open'));
  }

  ini_set ('track_errors', $tmp);
  return $result;
}

function GetChildren ($vals, &$i, $type)
{
  if ($type == 'complete') {
    if (isset ($vals [$i]['value']))
      return ($vals [$i]['value']);
    else
      return '';
  }

  $children = array (); // Contains node data

  // Loop through children
  while ($vals [++$i]['type'] != 'close') {
    $type = $vals [$i]['type'];
    // first check if we already have one and need to create an array
    if (isset ($children [$vals [$i]['tag']])) {
      if (is_array ($children [$vals [$i]['tag']])) {
        $temp = array_keys ($children [$vals [$i]['tag']]);
        // there is one of these things already and it is itself an array
        if (is_string ($temp [0])) {
          $a = $children [$vals [$i]['tag']];
          unset ($children [$vals [$i]['tag']]);
          $children [$vals [$i]['tag']][0] = $a;
        }
      } else {
        $a = $children [$vals [$i]['tag']];
        unset ($children [$vals [$i]['tag']]);
        $children [$vals [$i]['tag']][0] = $a;
      }

      $children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
    } else
      $children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
    // I don't think I need attributes but this is how I would do them:
    if (isset ($vals [$i]['attributes'])) {
      $attributes = array ();
      foreach (array_keys ($vals [$i]['attributes']) as $attkey)
      $attributes [$attkey] = $vals [$i]['attributes'][$attkey];
      // now check: do we already have an array or a value?
      if (isset ($children [$vals [$i]['tag']])) {
        // case where there is an attribute but no value, a complete with an attribute in other words
        if ($children [$vals [$i]['tag']] == '') {
          unset ($children [$vals [$i]['tag']]);
          $children [$vals [$i]['tag']] = $attributes;
        }
        // case where there is an array of identical items with attributes
        elseif (is_array ($children [$vals [$i]['tag']])) {
          $index = count ($children [$vals [$i]['tag']]) - 1;
          // probably also have to check here whether the individual item is also an array or not or what... all a bit messy
          if ($children [$vals [$i]['tag']][$index] == '') {
            unset ($children [$vals [$i]['tag']][$index]);
            $children [$vals [$i]['tag']][$index] = $attributes;
          }
          $children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
        } else {
          $value = $children [$vals [$i]['tag']];
          unset ($children [$vals [$i]['tag']]);
          $children [$vals [$i]['tag']]['value'] = $value;
          $children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
        }
      } else
        $children [$vals [$i]['tag']] = $attributes;
    }
  }

  return $children;
}

$d = GetXmlTree( file_get_contents( './cb-test.xml' ) );

var_dump( $d );

echo $d['spritpreise']['spritpreis'][0]['sorte']. ' , ';
echo $d['spritpreise']['spritpreis'][0]['preis']."\n";
echo $d['spritpreise']['spritpreis'][1]['sorte']. ' , ';
echo $d['spritpreise']['spritpreis'][1]['preis']."\n";
echo $d['spritpreise']['spritpreis'][2]['sorte']. ' , ';
echo $d['spritpreise']['spritpreis'][2]['preis']."\n";
Ausgabe wäre:
Code:
array(1) {
  ["spritpreise"]=>
  array(1) {
    ["spritpreis"]=>
    array(3) {
      [0]=>
      array(2) {
        ["sorte"]=>
        string(6) "sorte1"
        ["preis"]=>
        string(4) "1.40"
      }
      [1]=>
      array(2) {
        ["sorte"]=>
        string(6) "sorte2"
        ["preis"]=>
        string(4) "1.50"
      }
      [2]=>
      array(2) {
        ["sorte"]=>
        string(6) "sorte3"
        ["preis"]=>
        string(4) "1.60"
      }
    }
  }
}
sorte1 , 1.40
sorte2 , 1.50
sorte3 , 1.60
 
Zuletzt bearbeitet:
PHP:
<?php
$spritpreise = simplexml_load_file("http://www.tankstelle.de/preise.xml");
$count = count($spritpreise->spritpreis);

for($i = 0, $i <= $count, $i++) {
  echo '1 - '.$spritpreise->spritpreise[$i]['sorte'].' ,'.
  $spritpreise->spritpreis[$i]['preis'].' -'."\n";
}
?>
ungetestet, basierend auf Yuuris Idee.
 
danke an Yuuri und auch Eagle-PsyX-
habe jetzt Yuuris erste Idee genommen, der Code sieht zwar nicht so schön aus, wie der 2. funktioniert aber. :)
EDIT: Endstatus ist jetzt erstmal folgender:
PHP:
<?php
$spritpreise = simplexml_load_file("http://www.tankstelle.de/preise.xml");

echo '<center> <font size="7">';
echo 'Diesel kostet '.$spritpreise->spritpreis[0]['preis'].' € <br>';
echo 'Normalbenzin kostet '.$spritpreise->spritpreis[1]['preis'].' € <br>';
echo '<font color="#FF0000">Super kostet '.$spritpreise->spritpreis[2]['preis'].' € <br> </font>';
echo 'Superplus kostet '.$spritpreise->spritpreis[3]['preis'].' € <br>';
echo 'Autogas kostet '.$spritpreise->spritpreis[4]['preis'].' € <br>';
echo '</center> </font>';

?>
 
Zuletzt bearbeitet:
@Yuuri
Auch ich ziehe Objekte vor. :-)

@freak96
Nahja, der Vorteil meiner geänderten Version von Yurri sollte sein, dass es eine Schleife ist und somit übertragbar auf einen Benzinpreis oder 200 ohne das der Code größer wird ;) Funktioniert aber auch nur sinnvoll, solang das Array ['sorte'] auch einen sinnvollen Wert trägt.
 
@Eagle-PsyX-
Sagte doch dass dein Code eigentlich schöner ist. Da kann ich aber nicht einzelne Preise hervorheben oder?

Achja, kann es eigentlich sein, dass Funpic simplexml nicht unterstützt? Die Zahlenwerte werden dort nämlich nicht angezeigt.
 
Zurück
Oben