Jump to content


Photo

array -> data -> xml file... no output??


  • Please log in to reply
1 reply to this topic

#1 takethetrain

takethetrain
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 March 2006 - 02:56 PM

Hi,

I'm using a function to convert an array to a string (supposedly), and then write that to an external (XML) file. But for some reason, while the string displays correctly within the PHP file, it is not written to the external file. No errors come up.

Original array (shortened for viewing purposes):
Array
(
    [picture] => Array
        (
            [0] => Array
                (
                    [image] => images/image1.jpg
                    [thumb] => images/th_image1.jpg
                    [title] => Image 1 Title
                    [description] => image 1 description
                )

            [1] => Array
                (
                    [image] => images/th_image2.jpg
                    [thumb] => images/th_image2.jpg
                    [title] => Image 2 Title
                    [description] => image 2 description
                )

        )

) 


Convert array to XML string:
function array2xml($array) {
    print "<?xml version='1.0'?>\n\t<gallery>";
    while (list ($num, $tmp) = each ($array)) {
        print "\n\t\t<picture>";
           while (list ($key, $val) = each ($tmp)) {
             print "\n\t\t\t<$key>$val</$key>";
           }
        print "\n\t\t</picture>";
    }
    print "\n</gallery>";
}

Displayed in PHP file:
<?xml version='1.0'?>
    <gallery>
        <picture>
            <image>images/image1.jpg</image>
            <thumb>images/th_image1.jpg</thumb>
            <title>Image 1 Title</title>
            <description>image 1 description</description>
        </picture>
        <picture>
            <image>images/th_image2.jpg</image>
            <thumb>images/th_image2.jpg</thumb>
            <title>Image 2 Title</title>
            <description>image 2 description</description>
        </picture>
</gallery>

Write data to a file:
$xmlstr = array2xml($arr["picture"]);
$fp = fopen ('temp.xml', 'w');  //While the data is not written, the temp file is created successfully.
$write = fputs($fp,$xmlstr);
fclose ($fp); 

Sorry for the very long post. Thanks for your help!

-Brandy

#2 takethetrain

takethetrain
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 March 2006 - 05:22 PM

The fix, for anyone who might care:

function array2xml($array) {

    $output = "";
    if($array) $output .= "<?xml version='1.0'?>\r\n\t<gallery>";

    while (list ($num, $tmp) = each ($array)) {
        $output .= "\r\n\t\t<picture>";

        while (list ($key, $val) = each ($tmp)) {
            $output .= "\r\n\t\t\t<$key>$val</$key>";
            }

        $output .= "\r\n\t\t</picture>";

        }

    if($array) $output .= "\r\n</gallery>";
    return $output;
    }

Thanks anyway..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users