<?php
/**
* parses a ini file into a xml
* If $useSections is true, then evey setting must fall under a section.<br>For more information about the format of ini-files, have a look at the function <code>parse_ini_file()</code> in the php manual.
* @param string $filePath
* @param bool $useSections determine whether the ini-settings must be grouped by there sections
* @return DomDocument holding a xml representation of the ini-file
*/
function iniToXML($filePath, $useSections = false) {
$aIni = parse_ini_file($filePath, $useSections);
$dom = new DOMDocument('1.0', 'iso-8859-1');
$root = $dom->appendChild($dom->createElement('root'));
if (!$useSections) {
appendIniSettings($root, $aIni);
} else {
foreach ($aIni as $sectionName => $iniSettings) {
$section = $root->appendChild($dom->createElement('section'));
$section->setAttribute('name', $sectionName);
appendIniSettings($section, $iniSettings);
}
}
return $dom;
}
function appendIniSettings($element, $iniSettings) {
$doc = $element->ownerDocument;
foreach ($iniSettings as $prop => $value) {
$setting = $element->appendChild($doc->createElement('setting', $value));
$setting->setAttribute('name', $prop);
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Convert flat ini file into xml</title>
</head>
<body>
NOTE: this code requires at least php5!
<?php
/* EXAMPLE OF USAGE */
// Convert ini file into XML, with use of sections
$iniXML = iniToXML('sample.ini', true);
//use formatOuput to create a nice indention, but it is only used for aesthetical reasons
$iniXML->formatOutput = true;
// Now we can print the resulting xml
echo '<pre>'.htmlentities($iniXML->saveXML()).'</pre>';
// or write it to a file named sample.xml
file_put_contents('sample.xml', $iniXML->saveXML());
?>
</body>
</html>
;** FILE sample.ini
; Save this file as sample.ini in the same directory as the example php file.
; This is a sample configuration file
; Comments start with ';', as in php.ini
[database]
datebaseName = test_db
dabatebaseUser = userA
datebasePass = secure