XML Document generation using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

XML Document generation using PHP

Post by madhu »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,

Am trying to generate XML Document using PHP.

But am getting the following error:

[b]Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in c:\Inetpub\wwwroot\xml\sample1.php:18 Stack trace: #0 c:\Inetpub\wwwroot\xml\sample1.php(18): DOMDocument->createElement('0') #1 {main} thrown in c:\Inetpub\wwwroot\xml\sample1.php on line 18[/b]

Please suggest me how to overcome this issue...........

[u][b]CODE:[/b][/u]


[u][b]inc.php[/b][/u]

Code: Select all

<?php
$data_base="madhu";
$my_server="localhost";
$my_user="root";
$my_pass="";
?>



<?
$doc=new DomDocument('1.0','iso-8859-1');
$root=$doc->createElement('root');
$root=$doc->appendChild($root);
include('inc.php');         
mysql_connect($my_server,$my_user,$my_pass)
        or die("Uanble to connect to MYSQL");
mysql_select_db($data_base)
        or die("Failed opening database");
$sql="select * from article";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
 $art=$doc->createElement('article');
 $art=$root->appendChild($art);
 foreach($row as $fieldname => $fieldvalue)
        {
          $childname=$doc->createElement($fieldname);
          $childname=$art->appendChild($childname);
          $childvalue=$doc->createTextNode($fieldvalue);
          $childvalue=$childname->appendChild($value);
        }
}
echo $dis=$doc->saveXML();
?>

waiting for your valuable replies................

Thanks and regards
MADHU


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please use

Code: Select all

tags for php code as it's much more readable.
And please mark the line of code mentioned in the error message. It's easier to spot and avoids misunderstandings.

[quote]Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in c:\Inetpub\wwwroot\xml\sample1.php:18 Stack trace: #0 c:\Inetpub\wwwroot\xml\sample1.php(18): [b]DOMDocument->createElement('0')[/b] #1 {main} thrown in c:\Inetpub\wwwroot\xml\sample1.php on line 18[/quote]Element names composed only of digits are not allwed.
mysql_fetch_array by default returns an array containing both a numeric and an associative index for the same record data, i.e. your array $row looks like[quote]0=>'dataA'
'fieldA'=>'dataA'
1=>'dataB'
'fieldB'=>'dataB'
2=>'dataC'
'fieldC'=>'dataC'[/quote]You need to limit mysql_fetch_array to the associative index.

Code: Select all

while($row=mysql_fetch_array($result, MYSQL_ASSOC))
see http://de2.php.net/mysql_fetch_array
Post Reply