voipfreak
Goto Top

Problem beim anzeigen einer XML Datei

Hallo zusammen habe ein großes Problem.

Ich habe eine XML Datei die wie folgt aufgebaut ist.:

<?xml version="1.0" encoding="iso-8859-15"?>  
<anzeigen>
<anzeige>
<titel>Testeintrag</titel>
<firma>Testfirma</firma>
<ort>verschiedene Vertriebsregionen</ort>
<plz></plz>
<aufgabenbereich>Mauerbauen</aufgabenbereich>
<qualifikation>Testquali</qualifikation>
<link>http://testlink.de</link>
<datum>17.08.11</datum>
</anzeige>
</anzeigen>

in dieser Datei befinden sich einige Einträge die ich in php ausgeben möchte. Nun habe ich schon lange im Netz gesucht und habe folgendes Script gefunden;

<?
$anzeigen = simplexml_load_file('test.xml');  
 
for($i=0,$size=count($titel);$i<$size;$i++)
 
{
    echo $anzeigen->anzeige[$i]->titel, '<br />';;  
    echo $anzeigen->anzeige[$i]->firma, '<br />';;  
	echo $anzeigen->anzeige[$i]->Ort, '<br />';;  
	echo $anzeigen->anzeige[$i]->plz, '<br />';;  
	echo $anzeigen->anzeige[$i]->link, '<br />';;  
 
}
?>

Ich bekomme als Ausgabe nur eine leere Seite. Ich kann meinem Brain aber auch bei google etc. keinen stups geben woran das liegen könnte.
Könnte Ihr mir dabei helfen ?

Danke schonmal im voraus.

Gruß Voipfreak

Content-Key: 171951

Url: https://administrator.de/contentid/171951

Ausgedruckt am: 28.03.2024 um 12:03 Uhr

Mitglied: bytecounter
bytecounter 23.08.2011 um 20:05:37 Uhr
Goto Top
Das ist ganz einfach.;Welchen Wert soll denn $title in Zeile 4 haben?

Korrekterweise:

foreach ($anzeigen as $anzeige) {
    echo    $anzeige->$title
   ...
}
Mitglied: voipfreak
voipfreak 23.08.2011 um 21:23:28 Uhr
Goto Top
Hi bytecounter danke schonmal dass du dir zeit genommen hast,

ich versuche mich mal anders auszudrücken:

Es Handelt sich um Stellenanzeigen und ich möchte die Werte so auslesen wie Sie in der XML stehen also Titel, Firma, Ort usw.
Nach der einen Anzeige kommt halt die nächste.

die wieder Titel, Firma, Ort etc enthält.

Gruß voipfreak
Mitglied: dog
dog 23.08.2011 um 22:41:13 Uhr
Goto Top
Betrachten wir doch mal zwei Grundregeln der Programmierung:

  1. Variablen sind dann verfügbar, wenn sie das erste mal zugewiesen wurden
  2. Programme arbeiten ausschließlich von oben nach unten.

Und jetzt denk noch mal über die Frage von bytecounter nach:
Welchen Wert soll denn $titel in Zeile 4 haben?
Mitglied: Biber
Biber 23.08.2011 um 23:02:09 Uhr
Goto Top
@dog

Zitat von @dog:
Betrachten wir doch mal zwei Grundregeln der Programmierung:

  1. Variablen sind dann verfügbar, wenn sie das erste mal zugewiesen wurden
  2. Programme arbeiten ausschließlich von oben nach unten.

Und jetzt denk noch mal über die Frage von bytecounter nach:
> Welchen Wert soll denn $titel in Zeile 4 haben?
Meinst du jezz' beim ersten Programmaufruf oder eher allgemein? face-wink

Grüße
Biber
Mitglied: voipfreak
voipfreak 24.08.2011 um 01:19:59 Uhr
Goto Top
So habe es gelöst:
Nach fünf Tassen Kaffee und ner Packung Kippen habe ich
dieses Code gefunden und für meine zwecke modifiziert:

<?php
$daten = file_get_contents("test.xml"); // XML Datei einlesen  
$daten = explode("<anzeige>" , $daten); // Inhalt in ein Array zerlegen  

for ($zaehler = 1; $zaehler < count($daten); $zaehler++) {
ereg("<link>(.*)</link>" , $daten[$zaehler] , $link); // Link auslesen (wird in $link[1] gespeichert)  
ereg("<titel>(.*)</titel>" , $daten[$zaehler] , $titel); // Name u. Beschreibung auslesen  
ereg("<datum>(.*)</datum>" , $daten[$zaehler] , $datum); // Datum auslesen  
ereg("<plz>(.*)</plz>" , $daten[$zaehler] , $plz); // Name u. Beschreibung auslesen  
ereg("<firma>(.*)</firma>" , $daten[$zaehler] , $firma);  
ereg("<ort>(.*)</ort>" , $daten[$zaehler] , $ort);  

 // Link Name und Beschreibung in HTML-Tags einbauen und ausgeben
 echo "<dl>\n";  
 echo " <dt> <a href=\"" . trim($link[1]) . "\" target=blank>$titel[1]</a></dt>\n";  
 echo " <dt> $datum[1] </dt>\n";  
.... usw
 echo "</dl>\n";  
}
?>

Vielen Dank Bytecounter für deinen Beitrag.

Gruß Voipfreak
Mitglied: EvilMoe
EvilMoe 24.08.2011 um 07:52:42 Uhr
Goto Top
Hallo,

mache es dir nicht so umständig.
Ich habe eine Funktion die wandelt dir dein xml in einem Array um. Das sollte für deine zwecke genau das sein was du suchst statt mit "ereg" zu arbeiten.

<?php
/** 
 * xml2array() will convert the given XML text to an array in the XML structure. 
 * Link: http://www.bin-co.com/php/scripts/xml2array/ 
 * Arguments : $contents - The XML text 
 *                $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
 *                $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. 
 * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. 
 * Examples: $array =  xml2array(file_get_contents('feed.xml'));  
 *              $array =  xml2array(file_get_contents('feed.xml', 1, 'attribute'));  
 */ 
function xml2array($contents, $get_attributes=1, $priority = 'tag') {  
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {  
        //print "'xml_parser_create()' function not found!"; 
        return array();
    }

    //Get the XML parser of PHP - PHP must have this module for the parser to work
    $parser = xml_parser_create('');  
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http:{{comment_single_line_double_slash:2}} 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);

    if(!$xml_values) return;//Hmm...

    //Initializations
    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array; //Refference

    //Go through the tags.
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
    foreach($xml_values as $data) {
        unset($attributes,$value);//Remove existing values, or there will be trouble

        //This command will extract these variables into the foreach scope
        // tag(string), type(string), level(int), attributes(array).
        extract($data);//We could use the array by itself, but this cooler.

        $result = array();
        $attributes_data = array();
        
        if(isset($value)) {
            if($priority == 'tag') $result = $value;  
            else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode  
        }

        //Set the attributes too.
        if(isset($attributes) and $get_attributes) {
            foreach($attributes as $attr => $val) {
                if($priority == 'tag') $attributes_data[$attr] = $val;  
                else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'  
            }
        }

        //See tag status and do the needed.
        if($type == "open") {//The starting of the tag '<tag>'  
            $parent[$level-1] = &$current;
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data;  
                $repeated_tag_index[$tag.'_'.$level] = 1;  

                $current = &$current[$tag];

            } else { //There was another element with the same tag name

                if(isset($current[$tag])) {//If there is a 0th element it is already an array
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;  
                    $repeated_tag_index[$tag.'_'.$level]++;  
                } else {//This section will make the value an array if multiple tags with the same name appear together
                    $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
                    $repeated_tag_index[$tag.'_'.$level] = 2;  
                    
                    if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well  
                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];  
                        unset($current[$tag.'_attr']);  
                    }

                }
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;  
                $current = &$current[$tag][$last_item_index];
            }

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'  
            //See if the key is already taken.
            if(!isset($current[$tag])) { //New Key
                $current[$tag] = $result;
                $repeated_tag_index[$tag.'_'.$level] = 1;  
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;  


            } else { //If taken, put all things inside a list(array)
                if(isset($current[$tag]) and is_array($current[$tag])) {//If it is already an array...

                    // ...push the new element into that array.
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;  
                    
                    if($priority == 'tag' and $get_attributes and $attributes_data) {  
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;  
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;  

                } else { //If it is not an array...
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                    $repeated_tag_index[$tag.'_'.$level] = 1;  
                    if($priority == 'tag' and $get_attributes) {  
                        if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well  
                            
                            $current[$tag]['0_attr'] = $current[$tag.'_attr'];  
                            unset($current[$tag.'_attr']);  
                        }
                        
                        if($attributes_data) {
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;  
                        }
                    }
                    $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken  
                }
            }

        } elseif($type == 'close') { //End of tag '</tag>'  
            $current = &$parent[$level-1];
        }
    }
    
    return($xml_array);
}  

$file = file_get_contents('xml.xml');  

var_dump( xml2array($file, 0) );

?>


Output:
array(1) {
  ["anzeigen"]=>  
  array(1) {
    ["anzeige"]=>  
    array(8) {
      ["titel"]=>  
      string(11) "Testeintrag"  
      ["firma"]=>  
      string(9) "Testfirma"  
      ["ort"]=>  
      string(30) "verschiedene Vertriebsregionen"  
      ["plz"]=>  
      array(0) {
      }
      ["aufgabenbereich"]=>  
      string(10) "Mauerbauen"  
      ["qualifikation"]=>  
      string(9) "Testquali"  
      ["link"]=>  
      string(18) "http://testlink.de"  
      ["datum"]=>  
      string(8) "17.08.11"  
    }
  }
}
Mitglied: dog
dog 24.08.2011 um 09:21:38 Uhr
Goto Top

back-to-topWTF?!


Der erste Code vom TO war schon fast richtig und ihr fangt hier mit Konstrukten an, bei denen sich mir die Fußnägel aufrollen...