www.webdeveloper.com
Results 1 to 2 of 2

Thread: xml with php???

  1. #1
    Join Date
    Apr 2006
    Location
    Oslo, Norway
    Posts
    95

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

  2. #2
    Join Date
    Jun 2004
    Location
    4846′36″ N 910′48″ E
    Posts
    3,747
    example, output it as a list:

    PHP Code:
    <!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>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles