萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php 輸出xml 文檔實例

php 輸出xml 文檔實例

有時可以是有益的對當前數據庫教程模式轉儲。下面的腳本讀取MySQL數據庫和輸出的XML

描述模式架構。

首先,我們連接到MySQL數據庫和使用SHOW TABLES命令返回所有數據庫中的表。下一

步,我們遍歷每個表和返回每個使用SHOW場命令表中的字段。最後,我們提出了到XML

返回的所有信息。

有一個看一看代碼:


<?php
// database constants
// make sure the information is correct
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "password");
define("DB_NAME", "tutorials");

// connection to the database
$dbhandle = mysql教程_connect(DB_SERVER, DB_USER, DB_PASS)
   or die("Unable to connect to MySQL");

// select a database to work with
$selected = mysql_select_db(DB_NAME, $dbhandle)
   or die("Could not select examples");

// return all available tables
$result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle );

$tables = array();
while ($row = mysql_fetch_row($result_tbl)) {
   $tables[] = $row[0];
}

$output = "<?xml version="1.0" ?> ";
$output .= "<schema>";

// iterate over each table and return the fields for each table
foreach ( $tables as $table ) {
   $output .= "<table name="$table">";
   $result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

   while( $row1 = mysql_fetch_row($result_fld) ) {
      $output .= "<field name="$row1[0]" type="$row1[1]"";
      $output .= ($row1[3] == "PRI") ? " primary_key="yes" />" : " />";
   }

   $output .= "</table>";
}

$output .= "</schema>";

// tell the browser what kind of file is come in
header("Content-type: text/xml");
// print out XML that describes the schema
echo $output;

// close the connection
mysql_close($dbhandle);
?>

另一方法


$document = new DOMDocument('1.0');
$schemaNode = $document->createElement("schema");
$document->appendChild($schemaNode);

foreach ( $tables as $table ) {
$tableNode .= $document->createElement("table");
$schemaNode->appendChild($tableNode);
$result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

while( $row1 = mysql_fetch_row($result_fld) ) {
$fieldNode = $document->createElement("field");
$tableNode->appendChild($fieldNode);

$fieldNode->setAttribute("name", $row1[0]);
$fieldNode->setAttribute("type", $row1[1]);
if ($row1[3] == "PRI")
$fieldNode->setAttribute("primary_key", "yes");
}
}

...

echo $document->saveXML();

===========================

php 輸出word文檔

在此方法中您需要格式化的HTML / PHP頁面使用Word友好CSS和標頭信息添加到您的

PHP腳本。請確保您不使用因為一切外部樣式表應在相同的文件。

因此,用戶將被提示下載文件。這個文件將不會被100%的“原始”的Word文檔,但它

肯定會在MS Word中打開應用程序。你可以使用這個既用於Unix和Windows環境的方法

<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo "<html>";
echo "<meta http-equiv="Content-Type" content="text/html;

charset=Windows-1252">";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";
?>

方法二

方法2 - 使用COM對象

請注意,在服務器運行下面必須有MS Word中所述的代碼安裝。 COM將只能在Windows

上工作。

Word文檔保存到臨時目錄,然後送往通過readfile()函數來浏覽器

// Create new COM object – word.application
$word = new COM("word.application");

// Hide MS Word application window
$word->Visible = 0;

//Create new document
$word->Documents->Add();

// Define page margins
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';

// Define font settings
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 10;

// Add text
$word->Selection->TypeText("TEXT!");

// Save document
$filename = tempnam(sys_get_temp_dir(), "word");
$word->Documents[1]->SaveAs($filename);

// Close and quit
$word->quit();
unset($word);

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

// Send file to browser
readfile($filename);
unlink($filename);

copyright © 萬盛學電腦網 all rights reserved