Best method for writing XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Best method for writing XML

Post by aliasxneo »

I need the best method in PHP for creating XML. Essentially I need to turn a result from MySQL into XML. I'm really not sure the best way to go about doing this though. While DOM looks pleasing, it seems like there's a much simpler way to just go from a nice MySQL result to XML.

Any pointers? Thanks.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Best method for writing XML

Post by JAB Creations »

Your post is exceptionally vague so I'll give you the answer to the question you asked. It will most likely not be the answer you want. If it's not then take in to consideration how you ask your questions as it will determine what (if any) input you recieve back from others.

Code: Select all

<?php
// include what you need...
 
echo "<xml_element>$my_variable</xml_element>";
 
// or...
 
echo '<xml_element>'.$my_variable.'</xml_element>';
?>
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Re: Best method for writing XML

Post by aliasxneo »

JAB Creations wrote:Your post is exceptionally vague so I'll give you the answer to the question you asked. It will most likely not be the answer you want. If it's not then take in to consideration how you ask your questions as it will determine what (if any) input you recieve back from others.

Code: Select all

<?php
// include what you need...
 
echo "<xml_element>$my_variable</xml_element>";
 
// or...
 
echo '<xml_element>'.$my_variable.'</xml_element>';
?>
Well I thought it was rather clear, but I'll give it another shot I suppose.

I simply need a method for generating XML (preferrably in an OOP way). I don't want to produce anything statically because all data will be coming from a database which is dynamic. So essentially I need a way of formatting dynamic data into XML.

To give an example, how would I turn the structure of something like an array (better yet an array of associative arrays) into valid XML? Do I manually have to loop through each array, and then each associative array and try to produce the XML? Or is there some other method to simplify this?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Best method for writing XML

Post by Jonah Bron »

Well, if you could turn the DB info into an array, that would make it easy:

Code: Select all

$xml = new DOMDocument('1.0', 'UTF-8');// initalize DOM
$xml->formatOutput = true;// format output
$root = $xml->createElement('database');// create document root
foreach ($db_array as $row){// loop through rows
    $newRow = $xml->createElement('row');// create row element
    foreach ($row as $cell){// loop through cells in current row
        $newCell = $xml->createElement('cell', $cell->nodeValue);// create cell element with value of current cell
        $newRow->appendChild($newCell);// apply cell element to current row
    }
    $root->appendChild($newRow);// apply current row to root
}
$xml->appendChild($root);// apply root to xml document
$xml->save('output.xml');// save xml
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Re: Best method for writing XML

Post by aliasxneo »

Okay so I'm trying to loop through an array structure (associative arrays) trying to create the XML equivalent. Here's my attempt:

Code: Select all

<?php
 
class XMLGen
{
    private $data;
    
    private $xml;
    private $lastEle;
    
    function __construct($data)
    {
        $this->data = $data;
    }
    
    public function toXML()
    {
        $this->xml = new DOMDocument('1.0', 'UTF-8');
        
        $root = $this->xml->createElement("results");
        $this->lastEle = $root;
        $this->iterate($this->data);
        
        $this->xml->appendChild($root);
        
        return $this->xml->saveXML();
    }
    
    private function iterate($array)
    {
        foreach($array as $key => $value)
        {
            if (is_array($value))
            {
                if (is_numeric($key)) { continue; }
                
                $ele = $this->xml->createElement($key);
                $this->lastEle->appendChild($ele);
                $this->lastEle = $ele;
                
                $this->iterate($value);
            } else {
                $this->lastEle->appendChild($this->xml->createElement($key, $value));
            }
        }
    }
}
 
?>
And my test code:

Code: Select all

$data = array("quests" => array("quest1" => array("name" => "test", "id" => "1"), array("quest2" => array("name" => "test2", "id" => "2"))));
$gen = new XMLGen($data);
echo $gen->toXML();
And that produces:

Code: Select all

<results><quests><quest1><name>test</name><id>1</id></quest1></quests></results>
So as you can see it's not working quite right. It managed to get the first nested array, but if you'll notice in "quests" there is two arrays. Somehow my class only caught one and skipped the other. I'm not sure why though. Any idea?
Post Reply