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:
Expand|Select|Wrap|Line Numbers
- <LOG>
- <ENTRY>
- <DATE>07:03:47 AM (07:03:47)</DATE>
- <ERLVL>1</ERLVL>
- <TYPE>User</TYPE>
- <DEVICE>Control Page</DEVICE>
- <MSG>User pressed play Best of Waters video</MSG>
- </ENTRY>
- </LOG>
Expand|Select|Wrap|Line Numbers
- $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->createElement( "LOG" );
- $doc->appendChild( $r );
- foreach( $logs as $log )
- {
- $b = $doc->createElement( "ENTRY" );
- $date = $doc->createElement( "DATE" );
- $date->appendChild(
- $doc->createTextNode( $log['DATE'] )
- );
- $b->appendChild( $date );
- $erlvl = $doc->createElement( "ERLVL" );
- $erlvl->appendChild(
- $doc->createTextNode( $log['ERLVL'] )
- );
- $b->appendChild( $erlvl );
- $type = $doc->createElement( "TYPE" );
- $type->appendChild(
- $doc->createTextNode( $log['TYPE'] )
- );
- $b->appendChild( $type );
- $device = $doc->createElement( "DEVICE" );
- $device->appendChild(
- $doc->createTextNode( $log['DEVICE'] )
- );
- $b->appendChild( $device );
- $msg = $doc->createElement( "MSG" );
- $msg->appendChild(
- $doc->createTextNode( $log['MSG'] )
- );
- $b->appendChild( $msg );
- $r->appendChild( $b );
- }
- echo $doc->save('test.xml');