PHP Classes

File: file_test.php

Recommend this page to a friend!
  Classes of Mauricio Souto   Easy XML Writer   file_test.php   Download  
File: file_test.php
Role: Example script
Content type: text/plain
Description: Example of how to generate a xml file from an object created using the XML class
Class: Easy XML Writer
Compose and generate XML documents
Author: By
Last change: Add a new parameter to the "toString" fuction to support a diferent way to generate the XML result.
Date: 15 years ago
Size: 3,685 bytes
 

Contents

Class file image Download
<?php


/*-
 * Copyright (c) 2009 Spîria Software - www.spiria.com.uy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
 
/**
 * This class show a smple example of how to use the XML class
 * and save the xml structure in a file
 *
 * @author Mauricio Souto - Spîria Software - www.spiria.com.uy
 */

 
include_once("XML.php");
 
 
//always start creating a XML object
 
$xml = new XML();
 
 
//add a root node
 
$r = $xml->createRoot("categories");
 
 
//add the childs of the root file
 
$food = $r->addChild("category");
 
$food->addAttribute("name", "food", false);
 
$drink = $r->addChild("category");
 
$drink->addAttribute("name", "drink", false);
 
$dessert = $r->addChild("category");
 
$dessert->addAttribute("name", "dessert", false);
 
 
$art = $food->addChild("article");
 
$art->addAttribute("name", "chicken", false);
 
$art->addAttribute("price", "12", false);
 
$art = $food->addChild("article");
 
$art->addAttribute("name", "hot dog", false);
 
$art->addAttribute("price", "5", false);
 
$art = $food->addChild("article");
 
$art->addAttribute("name", "hamburger", false);
 
$art->addAttribute("price", "8", false);
 
 
$art = $drink->addChild("article");
 
$art->addAttribute("name", "coke", false);
 
$art->addAttribute("price", "3", false);
 
$art = $drink->addChild("article");
 
$art->addAttribute("name", "beer", false);
 
$art->addAttribute("price", "5", false);
 
$art = $drink->addChild("article");
 
$art->addAttribute("name", "water", false);
 
$art->addAttribute("price", "2", false);
 
 
$art = $dessert->addChild("article");
 
$art->addAttribute("name", "chocolate cake", false);
 
$art->addAttribute("price", "4.5", false);
 
$art = $dessert->addChild("article");
 
$art->addAttribute("name", "ice cream", false);
 
$art->addAttribute("price", "2", false);

 
//in this other case the atttributes of the node displays in the header of the node, like this <node nameAttribute=value/>
 
$xml->toFile("xml_files/", "catalog1.xml", false);
 
 
//in this case the atttributes of the node displays like the childs, like this <nameAttribute>value</nameAttribute>
 
$xml->toFile("xml_files/", "catalog2.xml", true);

?>