I have a MySQL database on my website, and I would like to know how I could get an XML output via PHP of the following column channels
in the table.
我的网站上有一个MySQL数据库,我想知道如何通过表格中以下列通道的PHP获取XML输出。
I want to make the xml output to something like this:
我想让xml输出到这样的东西:
<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.mysite.com/xmltv">
<channel id="">
<display-name>Information from database</display-name>
<programme channel="Information from database" start="" stop="">
<title lang="en"></title>
<sub-title lang="en">
</sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>
Here is the php code:
这是php代码:
<?php
function db_connect()
{
define('DB_HOST', 'localhost');
define('DB_USER', 'myusername');
define('DB_PASSWORD', 'mypasword');
define('DB_DATABASE', 'mydbname');
$errmsg_arr = array();
$errflag = false;
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
}
db_connect();
function clean($var)
{
return mysql_real_escape_string(strip_tags($var));
}
$channels = clean($_GET['channels']);
$id = clean($_GET['id']);
if($errflag)
{
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
echo implode('<br />',$errmsg_arr);
}
else
{
$insert = array();
if(isset($_GET['channels']))
{
$insert[] = 'channels = \'' . clean($_GET['channels']) .'\'';
}
if(isset($_GET['id']))
{
$insert[] = 'id = \'' . clean($_GET['id']) . '\'';
}
if($channels && $id)
{
$qrytable1="SELECT id, channels, links FROM tvguide WHERE channels='$channels' && id='$id'";
$result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
while ($row = mysql_fetch_array($result1))
{
...
}
mysql_close();
}
else if(!$channels && ! $id)
{
$qrytable1="SELECT id, channels, links, streams FROM tvguide";
$result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
echo '<?xml version=""1.0"" encoding="UTF-8" ?>';
echo '<tv generator-info-name="www.mysite.com/xmltv">';
echo '<channel id="">';
echo '<display-name></display-name>';
echo '<programme channel="" start="" stop="">';
echo '<title lang="en"></title>';
echo '<sub-title lang="en"></sub-title>';
echo '<desc lang="en"></desc>';
echo '<category lang="en"></category>';
echo '</programme>';
echo '</channel>';
while ($row = mysql_fetch_array($result1))
{
echo "<p id='channels'>".$row["id"]. " " . $row["channels"]. "</p>";
}
}
}
?>
I would really appreciate it if you could tell me how to do this. Please note; I am a complete noob at PHP and ANY supplied code will be of great help.
如果你能告诉我怎么做,我真的很感激。请注意;我是PHP的完整菜鸟,任何提供的代码都会有很大的帮助。
Edit: I'm generating an XML file to save in my web host, but I can't be able to read the XML file as I'm getting an error error on line 3 at column 1: Extra content at the end of the document
.
编辑:我正在生成一个XML文件以保存在我的Web主机中,但我无法读取XML文件,因为我在第1行第3行收到错误错误:结尾处的额外内容文献。
Here's the XML output:
这是XML输出:
<?xml version="1.0" encoding="UTF-8"?>
<tv generator-info-name="www.mysite.com/xmltv"/>
<channel><display-name>Information from database</display-name><programme/><desc/></channel>
2 个解决方案
#1
0
Here's some example code using PHP's built in classes to build an XML tree. This isn't 100% of what you're asking for, but it demonstrates how to create the structure, add children, add attributes, etc. Using this snippet, you can get there from here. I use indentation to help me keep the tree structure straight...
下面是一些使用PHP内置类构建XML树的示例代码。这不是您所要求的100%,但它演示了如何创建结构,添加子项,添加属性等。使用此片段,您可以从此处获取。我用缩进来帮助我保持树形结构的直线...
// create a dom document with encoding utf8
$domtree = new DOMDocument('1.0', 'UTF-8');
// create a root element of the xml tree
$tv = $domtree->createElement('tv');
//create attributes for element
$generator_info_name = $domtree->createAttribute('generator-info-name');
$generator_info_name->value = 'www.mysite.com/xmltv';
//append attribute
$tv->appendChild($generator_info_name);
// append element to the doc
$tv = $domtree->appendChild($tv);
//add a channel as a child of the root
$channel = $domtree->createElement('channel');
$channel_id = $domtree->createAttribute('id');
$channel_id->value = '""';
$channel = $tv->appendChild($channel);
//append children to channel
$channel->appendChild($domtree->createElement('display-name','Information from database'));
$channel->appendChild($domtree->createElement("programme"));
$channel->appendChild($domtree->createElement('desc'));
//finally, save the file
echo $domtree->saveXML();
$domtree->save('myChannel.xml');
#2
0
I'd suggest to use XMLWriter. http://www.php.net/manual/en/ref.xmlwriter.php.
我建议使用XMLWriter。 http://www.php.net/manual/en/ref.xmlwriter.php。
I see you use mysql... Instead I'd suggest to use mysqli to connect to the database, instead of the deprecated mysql. It is the improved extension.
我看到你使用mysql ...相反,我建议使用mysqli连接数据库,而不是弃用的mysql。这是改进的扩展。
You could do something like this:
你可以这样做:
$xml = new XMLWriter();
$xml->openMemory();
$xml->startDTD('xml');
$xml->endDTD();
$xml->startElement('tv');
$xml->startAttribute('generator-info-name');
$xml->text('www.mysite.com/xmltv');
$xml->endAttribute();
$xml->startElement('channel');
$xml->startAttribute('id');
$xml->text('');
$xml->endAttribute();
$xml->endElement();
$xml->endElement();
$xml->endElement();
header("Content-type: text/xml; charset=utf-8");
echo $xml->outputMemory();
Good luck with it :)
祝它好运:)
#1
0
Here's some example code using PHP's built in classes to build an XML tree. This isn't 100% of what you're asking for, but it demonstrates how to create the structure, add children, add attributes, etc. Using this snippet, you can get there from here. I use indentation to help me keep the tree structure straight...
下面是一些使用PHP内置类构建XML树的示例代码。这不是您所要求的100%,但它演示了如何创建结构,添加子项,添加属性等。使用此片段,您可以从此处获取。我用缩进来帮助我保持树形结构的直线...
// create a dom document with encoding utf8
$domtree = new DOMDocument('1.0', 'UTF-8');
// create a root element of the xml tree
$tv = $domtree->createElement('tv');
//create attributes for element
$generator_info_name = $domtree->createAttribute('generator-info-name');
$generator_info_name->value = 'www.mysite.com/xmltv';
//append attribute
$tv->appendChild($generator_info_name);
// append element to the doc
$tv = $domtree->appendChild($tv);
//add a channel as a child of the root
$channel = $domtree->createElement('channel');
$channel_id = $domtree->createAttribute('id');
$channel_id->value = '""';
$channel = $tv->appendChild($channel);
//append children to channel
$channel->appendChild($domtree->createElement('display-name','Information from database'));
$channel->appendChild($domtree->createElement("programme"));
$channel->appendChild($domtree->createElement('desc'));
//finally, save the file
echo $domtree->saveXML();
$domtree->save('myChannel.xml');
#2
0
I'd suggest to use XMLWriter. http://www.php.net/manual/en/ref.xmlwriter.php.
我建议使用XMLWriter。 http://www.php.net/manual/en/ref.xmlwriter.php。
I see you use mysql... Instead I'd suggest to use mysqli to connect to the database, instead of the deprecated mysql. It is the improved extension.
我看到你使用mysql ...相反,我建议使用mysqli连接数据库,而不是弃用的mysql。这是改进的扩展。
You could do something like this:
你可以这样做:
$xml = new XMLWriter();
$xml->openMemory();
$xml->startDTD('xml');
$xml->endDTD();
$xml->startElement('tv');
$xml->startAttribute('generator-info-name');
$xml->text('www.mysite.com/xmltv');
$xml->endAttribute();
$xml->startElement('channel');
$xml->startAttribute('id');
$xml->text('');
$xml->endAttribute();
$xml->endElement();
$xml->endElement();
$xml->endElement();
header("Content-type: text/xml; charset=utf-8");
echo $xml->outputMemory();
Good luck with it :)
祝它好运:)