2

I tried this code to create xml file

public function get_markers()
{
    $this->load->dbutil();
    $sql = "select name, address, lat, lng, type from restaurant where status=1";
    $query = $this->db->query($sql);
    $config = array (
        'root'    => 'markers',
        'element' => 'marker',
        'newline' => "\n",
        'tab'     => "\t"
    );
    $xml = $this->dbutil->xml_from_result($query, $config);
    $this->output->set_content_type('text/xml');
    $this->output->set_output($xml); 
}   

This code return following result:

<markers>
    <marker>
        <name>Soto Bangkong</name>
        <address>JL. Setiabudi No. 229 Srondol, Srondol Kulon</address>
        <lat>-7.06223759219975</lat>
        <lng>110.4129814497071</lng>
        <type>7</type>
    </marker>
    <marker>
        <name>Waroeng Semawis</name>
        <address>Jalan Gang Warung No. 50, Kauman</address>
        <lat>-6.974595476216744</lat>
        <lng>110.42663989422613</lng>
        <type>7</type>
    </marker>
</markers>

How to make xml file from controller in codeigniter, to add attribute inside node. To look exactly like this :

<markers>
<marker id="1" name="Billy Kwong" address="1/28 Macleay Street, Elizabeth Bay, NSW" lat="-33.869843" lng="-151.225769" type="restaurant"/>
<marker id="2" name="Love.Fish" address="580 Darling Street, Rozelle, NSW" lat="-33.861034" lng="151.171936" type="restaurant"/>
</markers>

2 Answers 2

1

try this

    function get_report(){
    $this->load->model('my_model');
    $this->load->dbutil();
    $this->load->helper('file');
    /* get the object   */
    $report = $this->my_model->index();
    /*  pass it to db utility function  */
    $new_report = $this->dbutil->xml_from_result($report);
    /*  Now use it to write file. write_file helper function will do it */
    write_file('xml_file.xml',$new_report);
    /*  Done    */
}

If you want to write csv file it is easy too. Just use csv_from_result() method of and use write_file('csv_file.csv,$new_report).

Sign up to request clarification or add additional context in comments.

Comments

0

There is a difference between xml element and attribute so you need to try this

public function display_xml()
    {
        $this->load->dbutil();
        $data = $this->db->query('select * from markers');

        $markers_data = $data->result_array();

        $this->output->set_content_type('text/xml');

        $dom = new DOMDocument("1.0");

        // create root element
        $root = $dom->createElement("markers");
        $dom->appendChild($root);

        foreach ($markers_data as $value) 
        {
            // create child element
            $marker = $dom->createElement("marker");
            $root->appendChild($marker);

            // create attribute node
            $id = $dom->createAttribute("id");
            $marker->appendChild($id);

            // create attribute value node
            $priceValue = $dom->createTextNode($value['id']);
            $id->appendChild($priceValue);

            // create attribute node
            $name = $dom->createAttribute("name");
            $marker->appendChild($name);

            // create attribute value node
            $nameValue = $dom->createTextNode($value['name']);
            $name->appendChild($nameValue);

            // create attribute node
            $address = $dom->createAttribute("address");
            $marker->appendChild($address);

            // create attribute value node
            $addressValue = $dom->createTextNode($value['address']);
            $address->appendChild($addressValue);

            // create attribute node
            $lat = $dom->createAttribute("lat");
            $marker->appendChild($lat);

            // create attribute value node
            $latValue = $dom->createTextNode($value['lat']);
            $lat->appendChild($latValue);

            // create attribute node
            $lng = $dom->createAttribute("lng");
            $marker->appendChild($lng);

            // create attribute value node
            $lngValue = $dom->createTextNode($value['lng']);
            $lng->appendChild($lngValue);

            // create attribute node
            $type = $dom->createAttribute("type");
            $marker->appendChild($type);

            // create attribute value node
            $typeValue = $dom->createTextNode($value['type']);
            $type->appendChild($typeValue);
        }

        // save and display tree
        echo $dom->saveXML();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.