/    Sign up×
Community /Pin to ProfileBookmark

xml with php???

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??

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 21.2006 — example, output it as a list:

[code=php]
<!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>
[/code]
×

Success!

Help @demiurgen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...