Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate xml in codeigniter

I am trying to generate xml from database query.

Here is generated xml link: http://mydeal.ge/api/xml

But when I try to parse this xml, I get an error: http://wallsparade.com/test.php.

My code is:

public function xml()
    {
        $res = $this->db->query('custom query');
        if($res->num_rows() > 0)
        {$output =  '<?xml version="1.0"?>'. "\n";
            $output .= "<deals>";
            foreach($res->result() as $item)
        {
            $output .= "<sale id = '".$item->id."'>";
            $output .= "<link>".$item->link."</link>";
            $output .= "<title>".urlencode($item->title)."</title>";
            $output .= "<image>".$item->image."</image>";
            $output .= "<text>".urlencode($item->text)."</text>";
            $output .= "<time>".$item->time."</time>";
            $output .= "<price>".$item->price."</price>";
            $output .= "<parcent>".$item->parcent."</parcent>";
            $output .= "</sale>";
        }
        $output .= '</deals>';
        }

        echo $output;

    }

What is problem?

like image 737
Gia Nebieridze Avatar asked Jul 25 '13 09:07

Gia Nebieridze


2 Answers

Looks like you are developing an API. Why don't you use the RESTServer plugin for CodeIgniter?

It's all handled for you and you don't have to worry about the format. You can choose to output in JSON or XML.

Plugin developed by Phil: https://github.com/philsturgeon/codeigniter-restserver

like image 99
Kong Jin Jie Avatar answered Sep 22 '22 17:09

Kong Jin Jie


I believe the most practical, logical and error free way to generate an XML is to create a DOMDocument as suggested by Eineki in this answer, allowing the xmltree to be searched through an xpath query.

With this said, some years ago Dan Simmons created a single MY_xml_helper.php that you can just copy to your application/helpers folder directly. Here's the entire code without comments:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('xml_dom'))
{
	function xml_dom()
	{
		return new DOMDocument('1.0', 'UTF-8');
	}
}

if ( ! function_exists('xml_add_child'))
{
	function xml_add_child($parent, $name, $value = NULL, $cdata = FALSE)
	{
		if($parent->ownerDocument != "")
		{
			$dom = $parent->ownerDocument;			
		}
		else
		{
			$dom = $parent;
		}
		
		$child = $dom->createElement($name);		
		$parent->appendChild($child);
		
		if($value != NULL)
		{
			if ($cdata)
			{
				$child->appendChild($dom->createCdataSection($value));
			}
			else
			{
				$child->appendChild($dom->createTextNode($value));
			}
		}
		
		return $child;		
	}
}


if ( ! function_exists('xml_add_attribute'))
{
	function xml_add_attribute($node, $name, $value = NULL)
	{
		$dom = $node->ownerDocument;			
		
		$attribute = $dom->createAttribute($name);
		$node->appendChild($attribute);
		
		if($value != NULL)
		{
			$attribute_value = $dom->createTextNode($value);
			$attribute->appendChild($attribute_value);
		}
		
		return $node;
	}
}


if ( ! function_exists('xml_print'))
{
	function xml_print($dom, $return = FALSE)
	{
		$dom->formatOutput = TRUE;
		$xml = $dom->saveXML();
		if ($return)
		{
			return $xml;
		}
		else
		{
			echo $xml;
		}
	}
}

Notice that you set the encoding like this: new DOMDocument('1.0', 'UTF-8');. Here's an example:

$this->load->helper('xml');
$dom = xml_dom();
$book = xml_add_child($dom, 'book');
xml_add_child($book, 'title', 'Hyperion');
$author = xml_add_child($book, 'author', 'Dan Simmons');        
xml_add_attribute($author, 'birthdate', '1948-04-04');
xml_add_child($author, 'name', 'Dan Simmons');
xml_add_child($author, 'info', 'The man that wrote MY_xml_helper'); 
xml_print($dom);

Would simply output:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <title>Hyperion</title>
    <author birthdate="1948-04-04">
     <name>Dan Simmons</name>
     <info>The man that wrote MY_xml_helper</info>
    </author>
</book>

The xml_print either echos or returns the $xml->saveXML().

Notice: you can still use the one and only function from the default XML helper from CodeIgniter: xml_convert("<title>'Tom' & \"Jerry\"") which just outputs: &lt;title&gt;&apos;Tom&apos; &amp; &quot;Jerry&quot;.

like image 24
Armfoot Avatar answered Sep 23 '22 17:09

Armfoot