|
|
|
<?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
[application]
caching = false
[other section]
property = value |
|
| XMLManipulation Categories : PHP, XML, SimpleXML | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | A very basic and fast XML parser Categories : PHP, PHP Classes, XML | | | Simple newsreader script Categories : PHP, XML, Rich Site Summary (RSS) | | | RSS parser using PHP5 and simpleXML Categories : Rich Site Summary (RSS), PHP, XML | | | Glossword - glossary compiler Categories : Content Management, PHP, MySQL, XML | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | XML easy parser Categories : PHP, XML | | | MySQL or SQL Query to XML Output Categories : PHP, MySQL, XML, Databases | | | XML Menu Categories : PHP, PHP Classes, Navigation, XML, XSL | | | Directory Listing To XML : Outputs XML File of a Given Directory Listing Categories : PHP, PHP Classes, XML, Directories | | | Trivia Quiz program using XML, XSLT and PHP Categories : PHP, XML, XSLT | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | SAPIPROCESSOR - is a compact XML-based CMS toolkit Categories : PHP, Content Management, XML | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | |
|
|
|