Introduction

Introduction – Passing the data to transform

Overview

There are three ways to pass the data for transforming to the object:

  • with a direct query

    This way you see in the first part of the introduction already.

  • with an existing DB_result object

    If you need the full power of the PEAR::DB or PEAR::MDB API, you should choose this way.

  • with an array

    This is not directly an SQL to XML transformation; if you pass an indice array to the instance, the keys of the array are transformed into XML tags, their values into the tag content. This feature his helpful for adding data to an XML transformed result set.

Direct query

This behavoir does connecting to the DBMS, quering and fetching he result automatically. XML_sql2xml requires an valid DSN as construtor parameter. The query has to be passed to the getXML() or add() method.

Take a look into the first part of the introduction for examples.

Passing an DB_Result object

PEAR::DB and PEAR::MDB return the result set of a query as DB_result object. You have to provide a DB_common instance to the construtor and the DB_result instance to getXML() or add().

Passing a DB_Result object

<?php
require_once "XML/sql2xml.php";
require_once 
"DB.php";

$db db::connect("mysql://username:password@localhost/xmltest");
$result $db->query("select * from bands");

$sql2xmlclass = new xml_sql2xml($db);
$xmlstring $sql2xmlclass->getxml($result);
?>

The XML output in $xmlstring is equal to the example in "The typical using".

This way is the only one, if you want to benefit from all the features of the database APIs.

Passing an array

If you pass an nested array to getXML() or add(), it will be transformed into an XML document.

Passing an array

<?php
require_once "XML/sql2xml.php";

$sql2xmlclass = new xml_sql2xml();

$array = array (
            array(
"name"=>"The Blabbers",
                  
"birth_year"=>"1998",
                  
"birth_place"=>"London",
                  
"genre"=>"Rock'n'Roll"),
            array(
"name"=>"Only Stupids",
                  
"birth_year"=>"1997",
                  
"birth_place"=>"New York",
                  
"genre"=>"hiphop")
);

$xmlstring $sql2xmlclass->getXML($array);
?>

The XML output in $xmlstring:

<?xml version="1.0"?>
    <root>
        <result>
            <row>
                <name>The Blabbers</name>
                <birth_year>1998</birth_year>
                <birth_place>London</birth_place>
                <genre>Rock'n'Roll</genre>
            </row>
            <row>
                <name>Only Stupids</name>
                <birth_year>1997</birth_year>
                <birth_place>New York</birth_place>
                <genre>hiphop</genre>
            </row>
        </result>
    </root>

Passing more then one result set

You can call add() several result sets to XML document. Just call the method for every result set; if you have added all result sets, call getXml() without any arguments to get the XML document.

Adding result sets

<?php
$sql2xmlclass
->add("select * from bands");
$sql2xmlclass->add("select * from albums");
$xmlstring$sql2xmlclass->getxml();
?>