Page 1 of 1

PHP4 XML DOM - write to document

Posted: Wed Feb 07, 2007 11:00 am
by joshlk
I am trying to write a php script which adds an element tagged ‘log’ to the root element ‘newletter_log’. I want it to have attributes added which are passed on via parameter variables from the function. I originally wrote the script for php5, however, I now need it to work in a php4 environment. My original script was:

Code: Select all

$xml_dom = new DOMDocument(); // Load DOM Object
$xml_dom -> load('db.xml');   // Load xml databse into the object	
$newsletter_log = $xml_dom -> getElementsByTagName('newsletter_log')->item(0); // Asign the element to a vaible
$newElement = $xml_dom -> createElement('log'); // Create a new elemnt and asign to varible
$newsletter_log -> appendChild($newElement);   // Apend the new element node into the parent element
$newElement -> setAttribute('action', $act);   // Setting new atriubts to the element
$newElement -> setAttribute('email', $email);
$newElement -> setAttribute('time/date', date("H:i d.m.y"));
$newElement -> setAttribute('recommended',$recommended);
$xml_dom -> save("db.xml");
This doesn’t work with php4. How can I convert the code to php4? Also, how can I find out if the host has DOMXML enabled?

Posted: Thu Feb 08, 2007 12:45 am
by Christopher
I believe that DomDocument is PHP5 only. You might want to look at places like phpclasses.org or sourceforge.net to see if there are PHP4 classes that do what you want.

Posted: Thu Feb 08, 2007 9:28 am
by feyd
The following can tell you what extensions they have enabled.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "
";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>