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 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line