php PHP中将MYSQL表数据直接转换为XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12576676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:50:42  来源:igfitidea点击:

Converting MYSQL table data directly to an XML in PHP

phpmysqlxml

提问by Mayank

I want to now if there is some inbuilt function that will create an XML directly from MYSQL result-set after executing SELECT query ?

我现在想知道是否有一些内置函数可以在执行 SELECT 查询后直接从 MYSQL 结果集创建一个 XML?

回答by Matthew Peel

I just wrote this and then thought id search to see if anyone else had written it. It looks simpler than the accepted answers tutorials. My $results comes from $result = mysql_query($query,$link);

我刚写了这个,然后想通过 id 搜索看看是否还有其他人写过它。它看起来比接受的答案教程更简单。我的 $results 来自 $result = mysql_query($query,$link);

$xmlDom = new DOMDocument();
$xmlDom->appendChild($xmlDom->createElement('results'));
$xmlRoot = $xmlDom->documentElement;

while ( $row = mysql_fetch_row($result) ) 
    {
      $xmlRowElementNode = $xmlDom->createElement('row');

      $i=0;
      for($i=0;$i<mysql_num_fields($result);$i++)
      {
          $xmlRowElement = $xmlDom->createElement(mysql_field_name($result,$i));
          $xmlText = $xmlDom->createTextNode($row[$i]);
            $xmlRowElement->appendChild($xmlText);

            $xmlRowElementNode->appendChild($xmlRowElement);
      }

      $xmlRoot->appendChild($xmlRowElementNode);
   }


    header('Content-type:  text/xml');
    echo $xmlDom->saveXML();

This will procude XML in the form of

这将以以下形式生成 XML

<results>
    <row1>
         <fieldname1>value</fieldname1>
         <fieldname2>value</fieldname2>
         <fieldname3>value</fieldname3>
         <fieldname4...>value</fieldname4...>
    </row1>
    <row2>
         <fieldname1>value</fieldname1>
         <fieldname2>value</fieldname2>
         <fieldname3>value</fieldname3>
         <fieldname4...>value</fieldname4...>
    </row2>
    <row3...>
         <fieldname1>value</fieldname1>
         <fieldname2>value</fieldname2>
         <fieldname3>value</fieldname3>
         <fieldname4...>value</fieldname4...>
    </row3...>
</results>

For any SELECT query.

对于任何 SELECT 查询。

回答by Yogesh Suthar

There is no inbuilt function that will create XML directly from MYSQL result-set after executing SELECT query.

没有内置函数可以在执行 SELECT 查询后直接从 MYSQL 结果集创建 XML。

You have to write code for this

你必须为此编写代码

Some nice tutorials are this..

一些不错的教程是这样的..

http://www.codediesel.com/php/converting-mysql-queries-to-xml/

http://www.codediesel.com/php/converting-mysql-queries-to-xml/

http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/

http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/

回答by mitnosirrag

Generally your MySQL result will be returned as an array or an object, which you can then convert to XML or another format. You can use SimpleXML, which has been explained here: How to convert array to SimpleXML

通常,您的 MySQL 结果将作为数组或对象返回,然后您可以将其转换为 XML 或其他格式。你可以使用 SimpleXML,这里已经解释了:How to convert array to SimpleXML