Click to See Complete Forum and Search --> : xml with php???


demiurgen
07-21-2006, 05:56 AM
with the "simplexml_load_file" function i load an xml file into a variable called $staff. and when i use the print_r function on the $staff variable i get this result:

SimpleXMLElement Object
(
[FirstName] => Array
(
[0] => James


[1] => John
[2] => Wayne
[3] => Jack
[4] => Eric
[5] => Roger

)

)
when i look at the xml data it is styled as this:
<names>
</FirstName>
<FirstName>James</FirstName>
<FirstName>John</FirstName>
<FirstName>Wayne</FirstName>
<FirstName>Jack</FirstName>
<FirstName>Eric</FirstName>
<FirstName>Roger</FirstName>
</names>


can anyone tell me how i can output this using a foreach loop??

LiLcRaZyFuZzY
07-21-2006, 06:07 AM
example, output it as a list:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">

</style>
</head>
<body>
<?php
# array xml2array(str filename)
function xml2array($xml_file){
# loading file
$xml = simplexml_load_file($xml_file);
if(!$xml){
# could not load file
return false;
}


# reading values from xml

foreach($xml->FirstName as $FirstName){
$xml_array[] = $FirstName;
}

# returning array
return $xml_array;
}

$names = xml2array("simple-xml.xml");

echo "<ul>";
foreach($names as $person){
echo "<li>$person</li>";
}
echo "</ul>";
?>
</body>
</html>