PHP Kodu:
<?php
$hostname_conn = "localhost";
$database_conn = "image_gallery";
$username_conn = "root";
$password_conn = "password";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?php
// Query the database and get all the records from the Images table
mysql_select_db($database_conn, $conn);
$query_rsImages = "SELECT ID, AlbumName, ImagePath, ImageDescription, UploadDate FROM images";
$rsImages = mysql_query($query_rsImages, $conn) or die(mysql_error());
$row_rsImages = mysql_fetch_assoc($rsImages);
$totalRows_rsImages = mysql_num_rows($rsImages);
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
?><?php echo('<?xml version="1.0" encoding="utf-8"?>'); ?>
<images>
<?php if ($totalRows_rsImages > 0) { // Show if recordset not empty ?>
<?php do { ?>
<image>
<ID><?php echo $row_rsImages['ID']; ?></ID>
<album><![CDATA[<?php echo $row_rsImages['AlbumName']; ?>]]></album>
<path><![CDATA[<?php echo $row_rsImages['ImagePath']; ?>]]></path>
<description><![CDATA[<?php echo $row_rsImages['ImageDescription']; ?>]]></description>
<date><![CDATA[<?php echo $row_rsImages['UploadDate']; ?>]]></date>
</image>
<?php } while ($row_rsImages = mysql_fetch_assoc($rsImages)); ?>
<?php } // Show if recordset not empty ?>
</images>
<?php
mysql_free_result($rsImages);
?>
Automatic: This version evaluates the query and automatically builds the nodes from the column names.
PHP Kodu:
<?php
$hostname_conn = "localhost";
$database_conn = "image_gallery";
$username_conn = "root";
$password_conn = "password";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?php
// Query the database and get all the records from the Images table
mysql_select_db($database_conn, $conn);
$query_rsAll = "SELECT * FROM images";
$rsAll = mysql_query($query_rsAll, $conn) or die(mysql_error());
$row_rsAll = mysql_fetch_assoc($rsAll);
$totalRows_rsAll = mysql_num_rows($rsAll);
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
?><?php echo('<?xml version="1.0" encoding="utf-8"?>'); ?>
<root>
<?php if ($totalRows_rsAll > 0) { // Show if recordset not empty ?>
<?php do { ?>
<row>
<?php foreach ($row_rsAll as $column=>$value) { ?>
<<?php echo $column; ?>><![CDATA[<?php echo $row_rsAll[$column]; ?>]]></<?php echo $column; ?>>
<?php } ?>
</row>
<?php } while ($row_rsAll = mysql_fetch_assoc($rsAll)); ?>
<?php } // Show if recordset not empty ?>
</root>
<?php
mysql_free_result($rsAll);
?>
ASP PHP Kodu:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim MM_conn_STRING
MM_conn_STRING = "dsn=image_gallery;uid=xxxx;pwd=xxxx"
%>
<%
Dim rsImages
Dim rsImages_cmd
Dim rsImages_numRows
' Query the database and get all the records from the Images table
Set rsImages_cmd = Server.CreateObject ("ADODB.Command")
rsImages_cmd.ActiveConnection = MM_conn_STRING
rsImages_cmd.CommandText = "SELECT ID, AlbumName, ImagePath, ImageDescription, UploadDate FROM images"
rsImages_cmd.Prepared = true
Set rsImages = rsImages_cmd.Execute
' Send the headers
Response.ContentType = "text/xml"
Response.AddHeader "Pragma", "public"
Response.AddHeader "Cache-control", "private"
Response.AddHeader "Expires", "-1"
%><?xml version="1.0" encoding="utf-8"?>
<images>
<% While (NOT rsImages.EOF) %>
<image>
<ID><%=(rsImages.Fields.Item("ID").Value)%></ID>
<album><![CDATA[<%=(rsImages.Fields.Item("AlbumName").Value)%>]]></album>
<path><![CDATA[<%=(rsImages.Fields.Item("ImagePath").Value)%>]]></path>
<description><![CDATA[<%=(rsImages.Fields.Item("ImageDescription").Value)%>]]></description>
<date><![CDATA[<%=(rsImages.Fields.Item("UploadDate").Value)%>]]></date>
</image>
<%
rsImages.MoveNext()
Wend
%>
</images>
<%
rsImages.Close()
Set rsImages = Nothing
%>
Automatic: This version evaluates the query and automatically builds the nodes from the column names.
PHP Kodu:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim MM_conn_STRING
MM_conn_STRING = "dsn=image_gallery;uid=xxxx;pwd=xxxx"
%>
<%
Dim rsAll
Dim rsAll_cmd
Dim rsAll_numRows
' Query the database and get all the records from the Images table
Set rsAll_cmd = Server.CreateObject ("ADODB.Command")
rsAll_cmd.ActiveConnection = MM_conn_STRING
rsAll_cmd.CommandText = "SELECT * FROM Images"
rsAll_cmd.Prepared = true
Set rsAll = rsAll_cmd.Execute
' Send the headers
Response.ContentType = "text/xml"
Response.AddHeader "Pragma", "public"
Response.AddHeader "Cache-control", "private"
Response.AddHeader "Expires", "-1"
%><?xml version="1.0" encoding="utf-8"?>
<root>
<% While (NOT rsAll.EOF) %>
<row>
<%
For each field in rsAll.Fields
column = field.name
%>
<<%=column%>><![CDATA[<%=(rsAll.Fields.Item(column).Value)%>]]></<%=column%>>
<%
Next
%>
</row>
<%
rsAll.MoveNext()
Wend
%>
</root>
<%
rsAll.Close()
Set rsAll = Nothing
%>
http://labs.adobe.com/technologies/s...query2xml.html