doukang5966907 2015-06-25 07:16
浏览 82

PHP将数据从mysql数据库输出到XML文件

I am getting data from mysql database to an XML file. I am using the following PHP code to get the output.

PHP CODE:

<?php

mysql_connect('localhost', 'root', '');
mysql_select_db('emptestdb');

$sql = "SELECT  name, username, DATE(visitdate) AS vdate, DATE(retdate) AS rdate, location FROM users";
$res = mysql_query($sql);

header("Content-Type: text/html/force-download");
header("Content-Disposition: attachment; filename='EMP_RECORDS.xml'");

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument('1.0','UTF-8');
$xml->setIndent(true);

$xml->startElement('engineers');

while ($row = mysql_fetch_assoc($res)) {
  $xml->startElement("engineer");

  $xml->writeAttribute('engID', $row['username']);
  $xml->writeAttribute('engName', $row['name']);

  $xml->startElement("location");
  $xml->writeAttribute('fieldLoc', $row['location']);

  $xml->writeElement('vistiDate',$row['vdate']);
  $xml->writeElement('retDate',$row['rdate']);

  $xml->endElement();
  $xml->endElement();
}

$xml->endElement();

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

$xml->flush();
?>

On using the above code i am getting the following output XML output.

Present Output:

<?xml version="1.0" encoding="UTF-8"?>

    <engineers>
     <engineer engID="1" engName="John">
      <location fieldLoc="Geneva">
       <vistiDate>2015-01-23</vistiDate>
       <retDate>2015-06-28</retDate>
      </location>
     </engineer>
     <engineer engID="1" engName="John">
      <location fieldLoc="Paris">
       <vistiDate>2015-02-12</vistiDate>
       <retDate>2015-02-17</retDate>
      </location>
     </engineer>
     <engineer engID="2" engName="Josh">
      <location fieldLoc="Paris">
       <vistiDate>2015-02-12</vistiDate>
       <retDate>2015-02-17</retDate>
      </location>
     </engineer>
    <engineer engID="3" engName="Rita">
      <location fieldLoc="Paris">
       <vistiDate>2015-02-12</vistiDate>
       <retDate>2015-02-17</retDate>
      </location>
     </engineer>
    </engineers>

I am trying to group the engID and get the following output

Desired Output:

<?xml version="1.0" encoding="UTF-8"?>
<engineers>
 <engineer engID="1" engName="John">
  <location fieldLoc="Geneva">
   <vistiDate>2015-01-23</vistiDate>
   <retDate>2015-06-28</retDate>
  </location>

  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
</engineer>

 <engineer engID="2" engName="Josh">
  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
 </engineer>

 <engineer engID="3" engName="Rita">
  <location fieldLoc="Paris">
   <vistiDate>2015-02-12</vistiDate>
   <retDate>2015-02-17</retDate>
  </location>
 </engineer>
</engineers>

Can someone guide me as to how to group it together. Thanks.

  • 写回答

1条回答 默认 最新

  • dongpu1908 2015-06-25 07:32
    关注

    A non-so bad idea should be to

    1. process your data and save it
    2. generate your xml

      $engineers = [];
      // process data
      while ($row = mysql_fetch_assoc($res)) {
           if (!isset($engineers[$row['username']])) {
               $engineers[$row['username']] = [
                    'name'      => $row['name'],
                    'locations' => [],
               ];
           }
           $engineers[$row['username']]['locations'][] = [
           $xml->writeAttribute('fieldLoc', $row['location']);
             'vistiDate' => $row['vdate'],
             'retDate'   => $row['rdate']);
           ];
      }
      // generate xml
      
      foreach ($engineers as $engineer) {
           // add engineer info to xml
           foreach ($engineer['locations'] as $location) {
                // add location to xml
           }
      }
      
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程