/    Sign up×
Community /Pin to ProfileBookmark

trying to create an XML file using php

Hi all,
I’m trying to create an xml file using php, and use it to get data from flash.
This is the code I’ve written so far and it doesn’t create an xml in my current directory. Could you tell me what am I doing wrong here?

php file:

[CODE]mysql_select_db($database_website_news, $website_news);
$query_all_news = “SELECT * FROM news”;
$all_news = mysql_query($query_all_news, $website_news) or die(mysql_error());
$row_all_news = mysql_fetch_assoc($all_news);
$totalRows_all_news = mysql_num_rows($all_news);

$_xml = “<?xml version=”1.0″ encoding=”utf-8″?>n<!DOCTYPE data[n
<!ELEMENT title(comments, image)>n
<!ATTLIST title name CDATA #REQUIRED>n
<!ELEMENT text (#PCDATA)>n
]>n
<data>n”;

do {
$_xml .= ‘<title name=”‘.$row_all_news[‘title’].'”>n’;
$_xml .= ‘<text>’.$row_all_news[‘text’].'</text>n’;
$_xml .= ‘</title>’;
} while ($row_all_news = mysql_fetch_assoc($all_news));

$_xml .= “</data>”;

//create the xml file and insert into the specific album’s folder
$file = ‘new.xml’;

$fh = fopen($file, ‘w’) or die(“can’t open file”);
fwrite($fh, $_xml);
fclose($fh);
[/CODE]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@SyCoDec 10.2008 — Turn up error handling to E_ALL

Post any errors/messages you get

Check the file you're wring to is writable.

I think you'll need the double quotes around the n or it'll treat it literally.
Copy linkTweet thisAlerts:
@ayveghDec 11.2008 — This never works in PHP:
[code=php]
$string = 'some text';
$string .= 'n';
[/code]


The n must be wrapped in double quotes, but this does work:
[code=php]
$string = 'some text' . "n";
[/code]


ayvegh
Copy linkTweet thisAlerts:
@ktsixitauthorDec 11.2008 — here's the code now, it's still not working but not returning errors too.

[CODE]function handleError() {
trigger_error('MY ERROR');
}
$dir_path = 'xml/';
$rs = @mkdir( $dir_path, '0777' );
@handleError();
if( $rs )
{}
else{
echo 'error';
echo '<br>dir_path: ' . $dir_path;
echo '<br>php_errormsg: ' . $php_errormsg;
}
$_xml = '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE data[
<!ELEMENT title(comments, image)>
<!ATTLIST title name CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>
]>
<data>';

do {
$_xml .= "<title>".$row_all_news['title']."</title>";
$_xml .= "text>".$row_all_news['text']."</text>";
//$_xml .= '</title>';
} while ($row_all_news = mysql_fetch_assoc($all_news));

$_xml .= "</data>";


$file = realpath($dir_path).'/new.xml';

$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);[/CODE]


You can see that I also changed and simplified the xml file that should be produced, so that I can be sure that the problem here is not because of string functions.

There is no xml created still. (folder->xml, file->new.xml)
Copy linkTweet thisAlerts:
@ayveghDec 11.2008 — I'm thinking that the fact that you're silently triggering an error may be a part of the problem.

Have you considered using one of the builtin PHP functions/extensions to create the XML file?

ayvegh
Copy linkTweet thisAlerts:
@UltimaterDec 11.2008 — text> should be <text>
<i>
</i>$_xml .= "[color=blue]&lt;[/color]text&gt;".$row_all_news['text']."&lt;/text&gt;";

Also
[code=php]
fwrite($fh, $content);
[/code]

should be writing $_xml not $content
Copy linkTweet thisAlerts:
@ktsixitauthorDec 11.2008 — somehow, someway it works ok now... I post the code here, in case it's usefull to somebody in the future. Thank you very much for your time and your help. I really appreciate it. :o

[CODE]$fname = "new.xml";
$fhandle = fopen($fname,"r");
$filestring = file_get_contents($fname);

$content = '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE data[
<!ELEMENT title(comments, image)>
<!ATTLIST title name CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>
]>
<data>';

do {
$content .= "n".'<title name="'.$_POST['title'].'">'."n";
$content .= "<text>".$_POST['text']."</text>"."n"."</title>"."nn";
} while ($row_all_news = mysql_fetch_assoc($all_news));

$content .= "</data>";

$newstring = substr_replace($filestring, $content, -7, 0);

$fhandle = fopen($fname,"w");
fwrite($fhandle,$newstring);
fclose($fhandle);[/CODE]
Copy linkTweet thisAlerts:
@SyCoDec 11.2008 — In my first reply I asked you to turn up error handling. If you had the $content variable would have shown an undefined variable error and Ultimater wouldn't have had to paw through your code to find the problem.

If you ask for help and someone responds asking you to do something and you don't then it becomes more difficult to help you.

BVefore posting you next problem please
Turn up error handling to E_ALL

Post any errors/messages you get[/QUOTE]


You will get faster more accurate help if you do.
Copy linkTweet thisAlerts:
@ayveghDec 11.2008 — Amen SyCo
Copy linkTweet thisAlerts:
@bealDec 12.2008 — I think you should have a look at the built in functions for creating xml in php. This'll save you a lot of coding in the future and minimizes the chance on errors. Also ensures your code stays up to date in the future.

The xml extension is documented here: http://nl3.php.net/manual/en/refs.xml.php

This tutorial was a great help for me and got me going in under 5 minutes: http://www.ibm.com/developerworks/library/x-xmlphp1.html
×

Success!

Help @ktsixit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...