Re: how to use php from mysql to xml

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 5, 2008 5:14 AM, Yang Yang <baozichn@xxxxxxxxx> wrote:

> hi,everyone,i am a newbuy for php world
>
> and i have a problem when i study php
>
>
> i want to make a script,it works for:
> a mysql table,like
>
> title     author    content    date
> a1        a2       a3         a4
> b1        b2       b3        b4
> ..........................
>
>
> and i want to use php ,select it and make a xml to save it ,now i use this
> script
>
> <?php
>
> header("Content-type: text/xml");
>
> $host = "localhost";
> $user = "root";
> $pass = "";
> $database = "test";
>
> $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to
> host.");
> mysql_select_db($database, $linkID) or die("Could not find database.");
>
> $query = "SELECT * FROM blog ORDER BY date DESC";
> $resultID = mysql_query($query, $linkID) or die("Data not found.");
>
> $xml_output = "<?xml version=\"1.0\"?>\n";
> $xml_output .= "<entries>\n";
>
> for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
>    $row = mysql_fetch_assoc($resultID);
>    $xml_output .= "\t<entry>\n";
>    $xml_output .= "\t\t<date>" . $row['date'] . "</date>\n";
>        // Escaping illegal characters
>        $row['text'] = str_replace("&", "&", $row['text']);
>        $row['text'] = str_replace("<", "<", $row['text']);
>        $row['text'] = str_replace(">", "&gt;", $row['text']);
>        $row['text'] = str_replace("\"", "&quot;", $row['text']);
>    $xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";
>    $xml_output .= "\t</entry>\n";
> }
>
> $xml_output .= "</entries>";
>
> echo $xml_output;
>
>  ?>
>
> it has no problem,but i want to save a xml file ,like this format
>
> <?xml version="1.0" encoding="GB2312"?>
> <Table>
>    <Record>
>        <Title>a1</Title>
>        <Author>a2</Author>
>        <Content>a3</Content>
>        <date>2003-06-29</date>
>    </Record>
>    <Record>
>        <Title>b1</Title>
>        <Author>b2</Author>
>        <Content>b3</Content>
>        <date>2003-06-30</date>
>    </Record>
>    ........
> ----many record
> </Table>
>
> how can i improve this script?


php has tools to help you work with xml, the best one for most purposes
in php5 is SimpleXML.
http://www.php.net/manual/en/ref.simplexml.php
heres the way i would do it (you may have debug a tiny bit, all ive done is
verified the syntax parses without errors).

<?php
header("Content-type: text/xml");

$host = "localhost";
$user = "root";
$pass = "";
$database = "test";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to
host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM blog ORDER BY date DESC";
if(!($resultSet = mysql_query($query, $linkID))) {
        die('data not found!');
}

/// create the table container
$table = new SimpleXMLElement('<table></table>');

while($resultRow = mysql_fetch_object($resultSet)) {
        $record = new SimpleXMLElement('<record></record');      // create a
container for the record
        foreach($resultRow as $curColName => $curColValue) {    // add the
columns from this record
                $record->addChild($curColName, $curColValue);
        }
        $table->addChild($record);
}
echo $table->asXML();


-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux