473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dom Document PHP

158 100+
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:
Expand|Select|Wrap|Line Numbers
  1. <LOG>
  2.   <ENTRY>
  3.     <DATE>07:03:47 AM (07:03:47)</DATE>
  4.     <ERLVL>1</ERLVL>
  5.     <TYPE>User</TYPE>
  6.     <DEVICE>Control Page</DEVICE>
  7.     <MSG>User pressed play Best of Waters video</MSG>
  8.   </ENTRY>
  9. </LOG>
  10.  
Here is my code of what ive done so far:
Expand|Select|Wrap|Line Numbers
  1.   $logs = array();
  2.   $logs [] = array(
  3.   'DATE' => '05/01/08',
  4.   'ERLVL' => '1',
  5.   'TYPE' => "System",
  6.   'DEVICE' => "Firefly",
  7.   'MSG' => "The Firefly did not respond to ping."
  8.   );
  9.   $logs [] = array(
  10.   'DATE' => '05/01/08',
  11.   'ERLVL' => '2',
  12.   'TYPE' => "User",
  13.   'DEVICE' => "Control Page",
  14.   'MSG' => "User pressed ping firefly"
  15.   );
  16.  
  17.   $doc = new DOMDocument();
  18.   $doc->load( 'test.xml' );
  19.   print_r($doc);
  20.  
  21.   $doc->formatOutput = true;
  22.  
  23.   $r = $doc->createElement( "LOG" );
  24.   $doc->appendChild( $r );
  25.  
  26.   foreach( $logs as $log )
  27.   {
  28.   $b = $doc->createElement( "ENTRY" );
  29.  
  30.   $date = $doc->createElement( "DATE" );
  31.   $date->appendChild(
  32.   $doc->createTextNode( $log['DATE'] )
  33.   );
  34.   $b->appendChild( $date );
  35.  
  36.   $erlvl = $doc->createElement( "ERLVL" );
  37.   $erlvl->appendChild(
  38.   $doc->createTextNode( $log['ERLVL'] )
  39.   );
  40.   $b->appendChild( $erlvl );
  41.  
  42.   $type = $doc->createElement( "TYPE" );
  43.   $type->appendChild(
  44.   $doc->createTextNode( $log['TYPE'] )
  45.   );
  46.   $b->appendChild( $type );
  47.  
  48.   $device = $doc->createElement( "DEVICE" );
  49.   $device->appendChild(
  50.   $doc->createTextNode( $log['DEVICE'] )
  51.   );
  52.   $b->appendChild( $device );  
  53.  
  54.   $msg = $doc->createElement( "MSG" );
  55.   $msg->appendChild(
  56.   $doc->createTextNode( $log['MSG'] )
  57.   );
  58.   $b->appendChild( $msg );  
  59.  
  60.   $r->appendChild( $b );
  61.   }
  62.  
  63.   echo $doc->save('test.xml');
  64.  
Really all i need to know is how to append the element LOG and add another entry to it.
May 1 '08 #1
1 1291
Atli
5,058 Expert 4TB
Hi.

Would something like this work?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Load the file
  3. $doc = new DOMDocument('1.0');
  4. $doc->load("test.xml");
  5.  
  6. // Get the first LOG note
  7. $nodes = $doc->getElementsByTagName("LOG");
  8.  
  9. if($nodes->length <= 0) 
  10.     die("Unable to find root note");
  11. else 
  12.     $root = $nodes->item(0);
  13.  
  14. // Append some stuff
  15. $entry = $root->appendChild(
  16.     $doc->createElement('ENTRY')
  17. );
  18.  
  19. $date = $entry->appendChild(
  20.     $doc->createElement('DATE')
  21. );
  22. $dateText = $date->appendChild(
  23.     $doc->createTextNode("2008-05-08")
  24. ); 
  25.  
  26. // Save
  27. $doc->save("test.xml");
  28. ?>
  29.  
May 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
2
by: Edward | last post by:
The below code builds 2 tables 4 rows by 4 cols. All cells have checkboxes. When checked, the checkboxes in the first column automatically check the remainder of the check boxes in the same row. ...
1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
5
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
6
by: therig | last post by:
I'm having issues, I've spent many hours searching and I'm a noob at javascript, any help will be greatly appreciated. I keep getting the following error: Error: document.forms.sec11_A has no...
4
by: dr1ft3r | last post by:
Hey guys, I'm building a site for a landscaping business down the street and can't seem to get part of the code functioning correctly. The code fails on line 68 where I make a reference to an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.