duanmin0941 2014-12-07 14:01
浏览 34
已采纳

处理完成后是否可以将.php转换为.xml?

I have a PHP Script that connects to a database, fetches results and presents them as an XML file.

<?php
header('Content-type: text/xml');

mysql_connect('host','user','password');
mysql_select_db('a8273293_blogger');

$sql = "Select * from Messages";
$q = mysql_query($sql) or die(mysql_error());

$result = mysql_query("SELECT COUNT(id) AS total FROM Messages"); // Query to count IDs
    $data = mysql_fetch_assoc($result); // Fetch query result
    $countNo = $data['total']; // Store count value in var $countNo

$dom = new DOMDocument();

$root = $dom->createElement('root'); // Root element
$dom->appendChild($root);

while($r = mysql_fetch_array($q)){
  $user = $dom->createElement('username'); // Define tag name
    $userContent = $dom->createTextNode($r['user_username']); // Field
    $user->appendChild($userContent); // Add content to tag

  $text = $dom->createElement('text');
    $textContent = $dom->createTextNode($r['text']);
    $text->appendChild($textContent);

  $posted_at = $dom->createElement('posted_at');
    $posted_atContent = $dom->createTextNode($r['posted_at']);
    $posted_at->appendChild($posted_atContent);

  $messages = $dom->createElement('messages'); // Container
    $messages->appendChild($user); // Add field
    $messages->appendChild($text);
    $messages->appendChild($posted_at);

     // Count

  $count = $dom->createElement('result'); // Define tag name
  $countContent = $dom->createTextNode($countNo); // Field
  $count->appendChild($countContent); // Add content to tag

  $root->appendChild($count); // Add blah
  $root->appendChild($messages); // Add content to root
}

$xmlString = $dom->saveXML();
echo $xmlString;

?>

This script creates an XML document which is all good but after the script is finished can i convert the file to XML? This is because I want to attach a Cron Job so that information can be gathered again after each day... This will eventually be used to feed information to an app.

Also is it better to try and concatenate or just create a new row with all the data I need in it?

For example if I had firstname, surname, title and I wanted everything from these 3 as one string in an XML tag as a pose to three could I just merge them?

Also if I only wanted my count function to output once could I place it outside of the while loop?

I appreciate this question is rather verbose but I am unsure about particular parts.

  • 写回答

1条回答 默认 最新

  • dongrong6235 2014-12-07 14:37
    关注

    instead of echo $xmlString; add this

    $myfile = fopen("output.xml", "w");
    fwrite($myfile, $xmlString);
    fclose($myfile);
    

    set the name and path for output.xml based on your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看