ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

MY_Xml_Writer - Easy XML writer library

March 10, 2009 9:12pm

Subscribe [13]
  • #31 / Mar 02, 2011 8:55am

    ukCIdeveloper

    6 posts

    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:

    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.

  • #32 / Mar 02, 2011 9:59am

    JoostV

    527 posts

    @ukCIdeveloper could you post the source code of file google.xml? Looks to me like you’ve got some output before

    <?xml version="1.0" encoding="UTF-8"?>
  • #33 / Mar 02, 2011 10:07am

    ukCIdeveloper

    6 posts

    Hey there,

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

    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

  • #34 / Mar 02, 2011 10:16am

    JoostV

    527 posts

    The problem may be here:

    $xml->getXml(true);

    This code echoes the XML to the screen. If you wish to collect only the data, do this instead:

    $xml->getXml();

    Also, you may want to wrap data like $product->product_name in CDATA tags:

    $$xml->addNode('title', $product->product_name, array(), TRUE);
  • #35 / Mar 02, 2011 10:33am

    ukCIdeveloper

    6 posts

    Thanks man. Seems to be fixed now and the xml file has been written. 😊

    Also remembered how to convert quotes to html entities:

    $xml->addNode('description', xml_convert($product->product_desc), array(), TRUE);
  • #36 / Mar 02, 2011 11:00am

    JoostV

    527 posts

    Good!

  • #37 / Mar 02, 2011 11:06am

    ukCIdeveloper

    6 posts

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

    <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/">
  • #38 / Mar 02, 2011 11:13am

    JoostV

    527 posts

    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?

  • #39 / Mar 02, 2011 11:19am

    ukCIdeveloper

    6 posts

    Sure, I would be happy to share it back so that others can use it. 😊

  • #40 / Mar 02, 2011 11:21am

    JoostV

    527 posts

    Thanks in advance!

  • #41 / Mar 02, 2011 2:03pm

    ukCIdeveloper

    6 posts

    Hey man, I’ve made some changes for a few things that are working well for me. Do you host on Github at all or have experiencing using it?

    I’m pretty new to it myself, but wondered about sharing my mods on there with you. Or I can just send over the changes for you to check out? 😊

    Regards,
    Neil

  • #42 / Mar 02, 2011 3:32pm

    JoostV

    527 posts

    Actually I do. Haven’t set up a repo for this lib, but I will. Get back to you when it’s done!

  • #43 / Mar 02, 2011 4:19pm

    JoostV

    527 posts

    Hi all,

    This class has moved to Github. Feel free to post a pull request if you have any additions.

    https://github.com/accent-interactive/xml_writer

  • #44 / Mar 02, 2011 6:37pm

    JoostV

    527 posts

    @Neil you can send me a pull request for Github. I’ll get a mention then, asking me to pull in your changes. Thanks for sharing!

  • #45 / Apr 26, 2011 8:30am

    neerose

    4 posts

    hey man thanks for you library file. its all working fine but i have some problem. while we write xml it converts all the html tags are encoded, i mean

    <div>Storyboard Artists</div>

    will be converted into
    <div><b>Storyboard Artists</b></div>
    <div>�</div>
    <div>
    but i do not want to encode while writing xml. can you suggest me please, how can i do it?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases