Hello all,
I am having trouble using the Dom Document classes in php5. Ill just go through a run down of what i need to accomplish, I need to be able to create a xml file and write to with log data with a few elements deep. When that is done it needs to be saved, but i need to be able to open it back up and add a entry to it.
I have figured out how to write to the file and save it using arrays as input, but after it is saved i cant figure out how to append the top element and add new childes.
Here is my xml structure:
[code=xml]
<LOG>
<ENTRY>
<DATE>07:03:4 7 AM (07:03:47)</DATE>
<ERLVL>1</ERLVL>
<TYPE>User</TYPE>
<DEVICE>Contr ol Page</DEVICE>
<MSG>User pressed play Best of Waters video</MSG>
</ENTRY>
</LOG>
[/code]
Here is my code of what ive done so far:
[CODE=php]
$logs = array();
$logs [] = array(
'DATE' => '05/01/08',
'ERLVL' => '1',
'TYPE' => "System",
'DEVICE' => "Firefly",
'MSG' => "The Firefly did not respond to ping."
);
$logs [] = array(
'DATE' => '05/01/08',
'ERLVL' => '2',
'TYPE' => "User",
'DEVICE' => "Control Page",
'MSG' => "User pressed ping firefly"
);
$doc = new DOMDocument();
$doc->load( 'test.xml' );
print_r($doc);
$doc->formatOutput = true;
$r = $doc->createElemen t( "LOG" );
$doc->appendChild( $r );
foreach( $logs as $log )
{
$b = $doc->createElemen t( "ENTRY" );
$date = $doc->createElemen t( "DATE" );
$date->appendChild(
$doc->createTextNode ( $log['DATE'] )
);
$b->appendChild( $date );
$erlvl = $doc->createElemen t( "ERLVL" );
$erlvl->appendChild(
$doc->createTextNode ( $log['ERLVL'] )
);
$b->appendChild( $erlvl );
$type = $doc->createElemen t( "TYPE" );
$type->appendChild(
$doc->createTextNode ( $log['TYPE'] )
);
$b->appendChild( $type );
$device = $doc->createElemen t( "DEVICE" );
$device->appendChild(
$doc->createTextNode ( $log['DEVICE'] )
);
$b->appendChild( $device );
$msg = $doc->createElemen t( "MSG" );
$msg->appendChild(
$doc->createTextNode ( $log['MSG'] )
);
$b->appendChild( $msg );
$r->appendChild( $b );
}
echo $doc->save('test.xml ');
[/CODE]
Really all i need to know is how to append the element LOG and add another entry to it.
I am having trouble using the Dom Document classes in php5. Ill just go through a run down of what i need to accomplish, I need to be able to create a xml file and write to with log data with a few elements deep. When that is done it needs to be saved, but i need to be able to open it back up and add a entry to it.
I have figured out how to write to the file and save it using arrays as input, but after it is saved i cant figure out how to append the top element and add new childes.
Here is my xml structure:
[code=xml]
<LOG>
<ENTRY>
<DATE>07:03:4 7 AM (07:03:47)</DATE>
<ERLVL>1</ERLVL>
<TYPE>User</TYPE>
<DEVICE>Contr ol Page</DEVICE>
<MSG>User pressed play Best of Waters video</MSG>
</ENTRY>
</LOG>
[/code]
Here is my code of what ive done so far:
[CODE=php]
$logs = array();
$logs [] = array(
'DATE' => '05/01/08',
'ERLVL' => '1',
'TYPE' => "System",
'DEVICE' => "Firefly",
'MSG' => "The Firefly did not respond to ping."
);
$logs [] = array(
'DATE' => '05/01/08',
'ERLVL' => '2',
'TYPE' => "User",
'DEVICE' => "Control Page",
'MSG' => "User pressed ping firefly"
);
$doc = new DOMDocument();
$doc->load( 'test.xml' );
print_r($doc);
$doc->formatOutput = true;
$r = $doc->createElemen t( "LOG" );
$doc->appendChild( $r );
foreach( $logs as $log )
{
$b = $doc->createElemen t( "ENTRY" );
$date = $doc->createElemen t( "DATE" );
$date->appendChild(
$doc->createTextNode ( $log['DATE'] )
);
$b->appendChild( $date );
$erlvl = $doc->createElemen t( "ERLVL" );
$erlvl->appendChild(
$doc->createTextNode ( $log['ERLVL'] )
);
$b->appendChild( $erlvl );
$type = $doc->createElemen t( "TYPE" );
$type->appendChild(
$doc->createTextNode ( $log['TYPE'] )
);
$b->appendChild( $type );
$device = $doc->createElemen t( "DEVICE" );
$device->appendChild(
$doc->createTextNode ( $log['DEVICE'] )
);
$b->appendChild( $device );
$msg = $doc->createElemen t( "MSG" );
$msg->appendChild(
$doc->createTextNode ( $log['MSG'] )
);
$b->appendChild( $msg );
$r->appendChild( $b );
}
echo $doc->save('test.xml ');
[/CODE]
Really all i need to know is how to append the element LOG and add another entry to it.
Comment