Ich habe eine .xml mit folgenden aufbau
Ich bin jetzt soweit das ich schon mal alles durchzählen kann und ausgeben bis auf die 'ingredient'
hier mal mein bisheriger grob hingeklatschter Code.
Kann mir jemand sagen wie ich jetzt an die Menge der 'ingredient' und den inhalt komme, stehe momentan auf den Schlauch.
mfg Spikaner
Code:
[21] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => bakedPotato
[count] => 1
[scrapable] => False
[craft_area] => campfire
[craft_tool] => cookingGrill
[craft_time] => 15
)
[ingredient] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => potato
[count] => 1
)
)
)
[22] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => antibiotics
[count] => 1
[scrapable] => False
[craft_area] => campfire
[craft_tool] => beaker
[craft_time] => 150
)
[ingredient] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => moldyBread
[count] => 1
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => potassiumNitratePowder
[count] => 1
)
)
)
)
hier mal mein bisheriger grob hingeklatschter Code.
PHP:
<?php
function xml_attribute($object, $attribute)
{
if(isset($object[$attribute]))
return (string) $object[$attribute];
}
$file="recipes.xml";
if (file_exists($file)) {
$inhalt=file_get_contents($file);
if (!empty($inhalt)) {
$xml = new SimpleXMLElement($inhalt);
// echo "<pre>";
// print_r($xml);
// echo "</pre>";
$zeilen = count ($xml->recipe);
// echo $zeilen;
for ($i = 0; $i < $zeilen; $i++){
$zeile = $xml->recipe->$i;
// kochgedönz
$pruf = xml_attribute($zeile, 'craft_area');
if (isset($pruf)) {
$name = xml_attribute($zeile, 'name');
echo $name."<br>";
// hier komme ich nicht weiter.
$zutat = count (xml_attribute($zeile, 'ingredient'));
echo $zutat."<br>";
}
}
// restliche ausgaben
}
else
{
echo "Kein Dateiinhalt.";
}
}
else
{
echo "Die Datei ".$file." existiert nicht";
}
?>
mfg Spikaner