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

标签 php mysql xml

我正在从 mysql 数据库获取数据到 XML 文件。我正在使用以下 PHP 代码来获取输出。

PHP 代码:

<?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();
?>

在使用上面的代码时,我得到以下输出 XML 输出。

当前输出:

<?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>

我正在尝试对 engID 进行分组并获得以下输出

期望的输出:

<?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>

有人可以指导我如何将它们组合在一起。 谢谢。

最佳答案

一个不太坏的想法应该是

  1. 处理您的数据并保存
  2. 生成你的 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
         }
    }
    

关于PHP从mysql数据库输出数据到XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043658/

相关文章:

php - curl 同一服务器时找不到 404

php - 使用 php ://input and file_put_contents

php - 无法找出这个正则表达式的解决方案

php - PHP:检查mysql中是否存在行

mysql - 为什么 mySQL 在我的查询中包含值为 0/0 的 client_num 列

php - 我应该在 MySQL 还是 PHP 中执行正则表达式过滤?

mysql - 从 artice Joomla 3 获取 sectionid

java - Rome RSS - 禁用 Rome RSS 中的验证

java - HTTP 状态 404 Servlet 重定向错误

c# - XSL 问题 XSL 选择特定字段名称