我尝试使用此代码创建xml文件
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);
}
此代码返回以下结果:
<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>
如何从codeignite r中的控制器制作xml文件,以在节点内添加属性。看起来完全像这样:
<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>
答案 0 :(得分:0)
试试这个
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 */
}
如果你想编写csv文件也很容易。 只需使用csv_from_result()方法并使用write_file(&#39; csv_file.csv,$ new_report)。
答案 1 :(得分:0)
xml元素和属性之间存在差异,因此您需要尝试一下
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();
}