#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    209
    Rep Power
    14

    Exclamation xml tree needs tweaking....


    Hey there,

    I am trying to develop a php file to log certain data into an xml file. The coding seems to work however the second time around the data is not saved on the xml file.

    Heres the code :

    $xml_string =

    "<errorlog>
    <report>
    <host>$host</host>
    <ipaddress>$ip</ipaddress>
    <errmsg>$subject</errmsg>
    <date>$dt</date>
    </report>
    </errorlog>
    ";

    and heres the error msg :

    Only one top level element is allowed in an XML document. Error processing resource 'file://wwwroot/errorlog.xml'. Line 9, Position 2


    <errorlog>
    -^

    The error i guess is because the second time around the script attempts to create the top level <errorlog> again. Is there a way to check for its presence or to avoid it from doing that ?!

    Appreciate your help !

    Urban Dev
  2. #2
  3. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Try this (assuming that your file contains <errorlog> and </errorlog> tags already):


    PHP Code:
    <?

    $report 
    "
    <report> 
    <host>
    $host</host> 
    <ipaddress>
    $ip</ipaddress> 
    <errmsg>
    $subject</errmsg> 
    <date>
    $dt</date> 
    </report>"
    ;

    $fp fopen("error_log.xml""r");
    $data fread($fpfilesize("error_log.xml"));

    $data str_replace ("</errorlog>",$report."</errorlog>",$data);

    fclose($fp);

    $fp2 fopen("error_log.xml""w");

    fputs($fp2$data);
    fclose($fp2);

    ?>
    Alex
    (http://www.alex-greg.com)

IMN logo majestic logo threadwatch logo seochat tools logo