Current location: Hot Scripts Forums » Programming Languages » PHP » Write in a XML file with php


Write in a XML file with php

Reply
  #1 (permalink)  
Old 03-13-06, 02:16 AM
pallabmondal123 pallabmondal123 is offline
Newbie Coder
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Write in a XML file with php

hi

i am trying to create an xml file with help of and the code:


PHP Code:

$query="Select * from ynfx_mis_news WHERE visibility='1' AND status ='a' order by id desc limit 20";

$result=mysql_db_query($DATABASE,$query);
$num = mysql_num_rows($result);
if ($num != 0)
{
    $file= fopen("polyester.xml", "wb");
    $_xml .="<?xml version='1.0' encoding='ISO-8859-1'?>\r\n";
    $_xml .="<rss version='1.0'>\r\n";
    $_xml .="<channel>\r\n";
    $_xml .="<title>YarnsandFibers Textile News</title>\r\n";
    $_xml .="<link>http://www.yarnsandfibers.com</link>\r\n";
    $_xml .="<description>YarnsandFibers Textile News</description>\r\n";

    $_xml .="<language>en</language>\r\n";
    $_xml .="<webMaster>bharati@yarnsandfibers.com</webMaster>\r\n";
    $_xml .="<copyright>YarnsandFibers</copyright>\r\n";
    $_xml .="<pubDate>".date("r")."</pubDate>\r\n";
    $_xml .="<lastBuildDate>".date("r")."</lastBuildDate>\r\n";

    $_xml .="<image>\r\n";
    $_xml .="<title>YarnsandFibers Textile News</title>\r\n";
    $_xml .="<url>http://www.yarnsandfibers.com/images/topbanner_logonew.gif</url>\r\n";
    $_xml .="<link>http://www.yarnsandfibers.com/news/</link>\r\n";
    $_xml .="</image>\r\n";

    for($i=0; $i<$num; $i++)
    {
        $id = mysql_result($result,$i,"id");
        $title = mysql_result($result,$i,"header");
        $description =substr(mysql_result($result,$i,"body"),0,200);
        $tag = array("<br>","<p>");
        $description = strip_tags($description,$tag);
        $date_news = mysql_result($result,$i,"date_display_from");


        $product  = mysql_result($result,$i,"product");
        //echo "$i : $product <br>";
        if($product != "N;")
        {
            $PROD_news = unserialize($product);
            $sql="select name from ynfx_mis_product where id = '$PROD_news[0]' and enable='1'";
            $result3=mysql_db_query($DATABASE,$sql);
            if(mysql_error()) error("Unexpected ERROR");
            $product = mysql_result($result3,0,"name");
        }
        $_xml .="<item>\r\n";
        $_xml .="<title>".str_replace($remove,"",$title)."</title>\r\n";
        $_xml .="<link>http://www.yarnsandfibers.com/news/index_fullstory.php3?id=$id</link>\r\n";
        $_xml .="<category>".$product."</category>\r\n";
        $_xml .="<pubDate>".date("r",$date_news)."</pubDate>\r\n";
        $remove = array("\x92", "\x96","\x97");
        $_xml .="<description>".str_replace($remove,"",$description)."...</description>\r\n";
        $_xml .="</item>\r\n";
    }
    $_xml .="</channel>\r\n";
    $_xml .="</rss>\r\n";
    fwrite($file, $_xml);
    fclose($file);
}
header("location:polyester.xml");
?>
but when i want to see the polyester.xml file that time showing me following error:


The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

XML document must have a top level element. Error processing resource 'http://yarnsandfibers.dev/rss/polyester.xml'.

Last edited by Christian; 04-16-06 at 01:15 PM. Reason: Please use [PHP][/PHP] when posting PHP code!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-13-06, 06:28 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
what webbrowser are you using?
not all webbrowsers support xml

try opening the xml-doc in windows, instead of through your webbrowser, and check if anything is written in it. It could be that your script works just perfectly

I also recommend you to use the php-xml functions, and not just create a xml-based string. read this

if you have any questions about it, just ask, i've written some xml-parsers before

Greetz
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-15-06, 06:34 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
Make sure you use htmlspecialchars() on all text output to the xml file!
PHP Code:

$_xml .="<description>".htmlspecialchars($descriptionENT_QUOTES)."...</description>\r\n";

//etc 
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP write to text file veeco Script Requests 2 02-10-10 03:48 PM
Write to php file with php script or create new file, it is possible? Oskare100 PHP 10 08-14-08 10:03 AM
How to write a php script to load a text file into a table automatically? xmxpcom PHP 2 02-12-04 09:37 AM
php write to file tiny Script Requests 2 01-14-04 10:12 PM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 05:28 PM


All times are GMT -5. The time now is 08:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.