CodeIgniter Forums
MY_Xml_Writer - Easy XML writer library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: MY_Xml_Writer - Easy XML writer library (/showthread.php?tid=16572)

Pages: 1 2 3 4 5 6


MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]ukCIdeveloper[/eluser]
Hey, I wondered if you might be able to check over my controller here. I'm using the library to create a google base feed, saving it as an xml file as follows:

My 'feeds' controller with the google function:

Code:
function google() {
        $this->load->helper('file');
        $this->load->model('products_model', '', TRUE);

        /*Fetch products from database*/
        $this->load->model('products_model');
        $products_model = $this->products_model->getAllProducts();
        
        // Load XML writer library
        $this->load->library('MY_Xml_writer');

        // Initiate class
        $xml = new MY_Xml_writer;
        $xml->setRootName('channel');
        $xml->initiate();

        /*Construct XML*/
        foreach ($products_model as $product) {
            $xml->startBranch('item');
            $xml->addNode('title', $product->product_name);
            $xml->addNode('link', $product->product_slug);
            $xml->addNode('guid', $product->product_slug);
            $xml->addNode('description', $product->product_desc);
            $xml->addNode('g:image_link', $product->product_image1);
            $xml->addNode('g:price', $product->product_price);
            $xml->addNode('g:condition', 'new');
            $xml->addNode('g:id', $product->product_id);
            $xml->endBranch();
        }

        if ( ! write_file('./application/views/feeds/google.xml', $xml->getXml(true)))
        {
             echo 'Unable to write the file';
        }
        else
        {
             echo 'File written!';
        }

So when I hit http://mydomain.com/feeds/google I would like it to save out the xml at http://mydomain.com/feeds/google.xml

However, when I run this I get the following error:

This page contains the following errors:

error on line 1 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

When I open the google.xml file, it does contain the products. I would be very grateful if you might be able to help fix the error?

Thank you.


MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]JoostV[/eluser]
@ukCIdeveloper could you post the source code of file google.xml? Looks to me like you've got some output before
Code:
<?xml version="1.0" encoding="UTF-8"?>



MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]ukCIdeveloper[/eluser]
Hey there,

Actually nothing seems to be writing to google.xml file now. The function is still as follows in my feeds controller;

Code:
function google() {
        $this->load->helper('file');
        $this->load->model('products_model', '', TRUE);
        
        /*Fetch products from database*/
        $this->load->model('products_model');
        $products_model = $this->products_model->getAllProducts();

        // Load XML writer library
        $this->load->library('MY_Xml_writer');

        // Initiate class
        $xml = new MY_Xml_writer;
        $xml->setRootName('channel');
        $xml->initiate();

        /*Construct XML*/
        foreach ($products_model as $product) {
            $xml->startBranch('item');
            $xml->addNode('title', $product->product_name);
            $xml->addNode('link', $product->product_slug);
            $xml->addNode('guid', $product->product_slug);
            $xml->addNode('description', $product->product_desc);
            $xml->addNode('g:image_link', $product->product_image1);
            $xml->addNode('g:price', $product->product_price);
            $xml->addNode('g:condition', 'new');
            $xml->addNode('g:id', $product->product_id);
            $xml->endBranch();
        }

        if ( ! write_file('./application/views/feeds/google.xml', $xml->getXml(true)))
        {
             echo 'Unable to write the file';
        }
        else
        {
             echo 'File written!';
        }
    }

When I hit http://mydomain.com/feeds/google, the page just remains blank. The google.xml file has full write permissions, as does the containing folder. Not quite sure why it worked once with an error, but now won't work at all. :S


MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]JoostV[/eluser]
The problem may be here:
Code:
$xml->getXml(true);

This code echoes the XML to the screen. If you wish to collect only the data, do this instead:
Code:
$xml->getXml();

Also, you may want to wrap data like $product->product_name in CDATA tags:
Code:
$$xml->addNode('title', $product->product_name, array(), TRUE);



MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]ukCIdeveloper[/eluser]
Thanks man. Seems to be fixed now and the xml file has been written. Smile

Also remembered how to convert quotes to html entities:

Code:
$xml->addNode('description', xml_convert($product->product_desc), array(), TRUE);



MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]JoostV[/eluser]
Good!


MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]ukCIdeveloper[/eluser]
Just trying to figure out how to modify your library for google. As google requires this below the xml version:

Code:
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:content="http://purl.org/rss/1.0/modules/content/">



MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]JoostV[/eluser]
It's currently nog in the class. However, this class extends PHP xmlwriter: http://php.net/manual/en/book.xmlwriter.php

Maybe you could write a method for it and post it back here?


MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]ukCIdeveloper[/eluser]
Sure, I would be happy to share it back so that others can use it. Smile


MY_Xml_Writer - Easy XML writer library - El Forum - 03-02-2011

[eluser]JoostV[/eluser]
Thanks in advance!