Question

I am trying to make an xml file to export my data from my database to my iphone. Each time I create a new post, I need to execute a php file to create a xml file containing the latest post ;)

Okay so far ? :D

Here is current php code ... but my nsxmlparser gives me an error code (33 - String is not started). I have no idea what kind of php functions I have to use ...

<?php

// Èdition du dÈbut du fichier XML
$xml .= '<?xml version=\"1.0\" encoding=\"UTF-8\"?>';
$sml .= '<channel>'; 
$xml .= '<title>Infonul</title>';
$xml .= '<link>aaa</link>';
$xml .= '<description>aaa</description>';


// connexion a la base (mettre ‡ jour le mdp)
$connect = mysql_connect('...-12','...','...');


/* selection de la base de donnÈe mysql */
mysql_select_db('...');


// selection des 20 derniËres news
$res=mysql_query("SELECT u.display_name as author,p.post_date as date,p.comment_count as commentCount, p.post_content as content,p.post_title as title FROM wp_posts p, wp_users u WHERE p.post_status = 'publish' and p.post_type = 'post' and p.post_author = u.id ORDER BY p.id DESC LIMIT 0,20");




// extraction des informations et ajout au contenu
while($tab=mysql_fetch_array($res)){   

 $title=$tab[title];
 $author=$tab[author];
 $content=$tab[content]; //html stuff
 $commentCount=$tab[commentCount];
 $date=$tab[date];

 $xml .= '<item>';
 $xml .= '<title>'.$title.'</title>';
 $xml .= '<content><![CDATA['.$content.']]></content>';
 $xml .= '<date>'.$date.'</date>';
 $xml .= '<author>'.$author.'</author>';
 $xml .= '<commentCount>'.$commentCount.'</commentCount>';
 $xml .= '</item>'; 
}

// Èdition de la fin du fichier XML
$xml .= '</channel>';
$xml = utf8_encode($xml);

echo $xml;

// Ècriture dans le fichier
if ($fp = fopen("20news.xml",'w'))
{
 fputs($fp,$xml);
 fclose($fp);
}

//mysql_close();

?>

I noticed a few things when i open 20news.xml in my browser :

  • I got squares instead of single quotes ...
  • I can't see <[CDATA[ but ]]> is clearly visible ... why ?!?

Thanks for any input ;)

Gotye.

Was it helpful?

Solution

Maybe you should use an XML library, e.g. php's DOM API, instead of string concatenation. Saves a lot of headaches.

Moreover, XML files need a single root element that contains all the other elements in order to be well-formed (see Wikipedia).

OTHER TIPS

Here is a sample code generated by my script ;)

I copy pasted the code from the source code in the browser ;)

  <?xml version=\"1.0\" encoding=\"UTF-8\"?>
<title>Infonul</title>
<link>aaa</link>
<description>aaa</description>
<item>
<title>Bing évolue et s'anime</title>
<content>
<![CDATA[<p style="text-align: center;"><a href="kkk"><img class="aligncenter size-medium wp-image-548" title="bing" src="http://www.infonul.com/wp-content/uploads/2010/03/bing-300x213.png" alt="" width="300" height="213" /></a></p>
    Vous êtes de plus en plus à l'utiliser, ce moteur de recherche lancé par Microsoft évolue dans sa version française pour ressembler un peu plus à la version US.<!--more--> Comment ? A travers les images de fond qui avaient déjà pour habitude de changer chaque jour vous pourrez maintenant "interagir" avec l'image sur certains points qui pointent sur un site en rapport direct avec l'illustration. Une façon un peu original de présenter le moteur de recherche.]]>
</content>
<date>2010-03-03 23:23:15</date>
<author>kkk kk</author>
<commentCount>0</commentCount>
</item>

<item>
<title>Du quadruple play chez Orange ?</title>
<content>...</content><date>2010-02-28 20:32:13</date><author>...</author><commentCount>0</commentCount>
</item>
</channel>
<?xml version="1.0" encoding="UTF-8"?>
<news>
  <item>
    <author>...</author>
    <title>Bing �volue et s'anime</title>
    <content><![CDATA[<p style="text-align: center;"><a href="..."><img class="aligncenter size-medium wp-image-548" title="bing" src="http://www.infonul.com/wp-content/uploads/2010/03/bing-300x213.png" alt="" width="300" height="213" /></a></p>
Vous �tes de plus en plus � l'utiliser, ce moteur de recherche lanc� par Microsoft �volue dans sa version fran�aise pour ressembler un peu plus � la version US.<!--more-->�Comment ? A travers les images de fond qui avaient d�j� pour habitude de changer chaque jour vous pourrez maintenant "interagir" avec l'image sur certains points�qui pointent sur�un site�en rapport direct avec l'illustration. Une fa�on un peu original de pr�senter le moteur de recherche.]]></content>
  </item>
  <item>
    <author>...</author>

    <title>Du quadruple play chez Orange ?</title>
    <content><![CDATA[<p style="text-align: center;"><a href="http://www.infonul.com/wp-content/uploads/2010/03/logo_orange_print1.gif"><img class="aligncenter size-full wp-image-543" title="orange logo" src="http://www.infonul.com/wp-content/uploads/2010/03/logo_orange_print1.gif" alt="" width="207" height="207" /></a></p>
�

Vous en r�viez ? C'est pour bient�t ! A l'annonce de ses r�sultats pour 2009, le groupe en a profit� pour laisser passer quelques messages � ce sujet.<!--more-->�En effet, Free s'appr�te � d�barquer sur le march� mobile et - il faut le dire - Orange ne propose�RIEN d'avantageux si vous avez votre box et votre mobile chez eux contrairement � Bouygues qui propose ses formules IDEO � des tarifs comp�titifs !Pour quand ? "D�ici � l'�t�"... Affaire � suivre.]]></content>
  </item>
</news>

That's what I get using DOMXML with php5... my php script is below ;)

<?php

//phpinfo();

$dom = new DOMdocument('1.0', 'UTF-8');
$dom->formatOutput = true;

$r = $dom->createElement('news');
$dom->appendChild( $r );

// connexion a la base (mettre ‡ jour le mdp)
$connect = mysql_connect('...-12','...','...');


/* selection de la base de donnÈe mysql */
mysql_select_db('...');


// selection des 20 derniËres news
$res=mysql_query("SELECT u.display_name as author,p.post_date as date,p.comment_count as commentCount, p.post_content as content,p.post_title as title FROM wp_posts p, wp_users u WHERE p.post_status = 'publish' and p.post_type = 'post' and p.post_author = u.id ORDER BY p.id DESC LIMIT 0,20");


// extraction des informations et ajout au contenu
while($tab=mysql_fetch_array($res)){ 

  $b = $dom->createElement( "item" );

  $author = $dom->createElement( "author" );
  $author->appendChild(
  $dom->createTextNode( $tab['author'] )
  );
  $b->appendChild( $author );

  $title = $dom->createElement( "title" );
  $title->appendChild(
  $dom->createTextNode( $tab['title'] )
  );
  $b->appendChild( $title );

  $content = $dom->createElement( "content" );
  $content->appendChild(
  $dom->createCDATASection($tab['content'])
  );
  $b->appendChild( $content );

  $r->appendChild( $b );

  }

  echo $dom->saveXML();
  ?>

If you have shown your php code exactly as it appears in your own file, you could try to change the line reading

$sml .= '<channel>';

to

$xml .= '<channel>';

It appears that you missed the starting tag, which got assigned to a different variable by mistake.

Claus

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top