PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Mikael Göransson   Recursive array to XML   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: A short example of what the class is meant to do
Class: Recursive array to XML
Generate XML documents from nested arrays
Author: By
Last change: Add example data for the new way of handling numeric keys as unassociated arrays.
Date: 16 years ago
Size: 2,170 bytes
 

Contents

Class file image Download
<?php
// Include the class (using require_once just for good measure)
require_once('xml.php');

// This is just dummy data to show what the class does
$exampleData = array(
                   
'root' => array(
                               
'foobar' => 'foovalue',
                               
'flump' => array('doh'),
                               
'hedk option="something"' => array(
                                                               
'gnu' => 'gnu is not unix',
                                                               
'linux' => 'linux is gnu'
                                                           
),
                               
'1plutt' => 'To create multiple tags with the same name, start with a number',
                               
'2plutt' => 'This is the second tag with the same name, but in the array it\'s starting with a different number',
                               
'numeric' => 'Numeric values are not valid in XML tags, so we treat them as unassociated arrays, creating a tag of the value.',
                               
'1' => 'numeric_tag',
                               
9 => 'another_numeric_tag',
                            ),
                );

// Initiate the class
$xml = new xml();

// Set the array so the class knows what to create the XML from
$xml->setArray($exampleData);

// Print the XML to screen
$xml->outputXML('echo');
/**
 * This will create:
 * <?xml version="1.0" encoding="ISO-8859-1"?>
 * <root>
 * <foobar>foovalue</foobar>
 * <flump>
 * <doh />
 * </flump>
 * <hedk option="something">
 * <gnu>gnu is not unix</gnu>
 * <linux>linux is gnu</linux>
 * </hedk>
 * <plutt>To create multiple tags with the same name, start with a number</plutt>
 * <plutt>This is the second tag with the same name, but in the array it&apos;s starting with a different number</plutt>
 * <numeric>Numeric values are not valid in XML tags, so we treat them as unassociated arrays, creating a tag of the value.</numeric>
 * <numeric_tag />
 * <another_numeric_tag />
 * </root>
 *
 */
?>