FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php and xml





garionw
Heres my problem. I have a PHP and an XML file. The XML file is actually a PHP file that goes into mysql and fetches data. The PHP script reads the XML file and parses it.

I can get it to work fine on a static XML document, but I get this error with the dynamic one:
Quote:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 13: parser error : Start tag expected, '<' not found in D:\www\viewxml.php on line 3

Warning: simplexml_load_string() [function.simplexml-load-string]: if(mysqli_num_rows($query) != 0)echo "<?xml version='1.0' standalone='yes'?>\n in D:\www\viewxml.php on line 3

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in D:\www\viewxml.php on line 3


my xml file:
Quote:
Code:
<?php
// Connect
 include('secure/connect.php');
 global $member, $connect;
 
 // Get all Events
  $query = mysqli_query($connect, 'SELECT * FROM wall_events, wall_actions WHERE wall_events.event_type = wall_actions.action_id');

 // Header
  header("Content-type: text/xml");
 
 // Loop it
  if(mysqli_num_rows($query) != 0)echo "<?xml version='1.0' standalone='yes'?>\n<events>";
  while($event = mysqli_fetch_array($query)){
    if($event['event_action'] != '')$description = $event['event_action'];
    else $description = $event['action_description'];
    echo '
 <event>
  <authorid>'. $event['event_author']. '</authorid>
  <url>'. $event['event_url']. '</url>
  <date>'. $event['event_date']. '</date>
  <time>'. $event['event_time']. '</time>
  <description>'. $description. '</description>
  <type>'. $event['action_name']. '</type>
 </event>
';
  }
  if(mysqli_num_rows($query) != 0)echo "</events>";
?>


When viewed in Firefox (truncated):
Quote:
Code:

<?xml version='1.0' standalone='yes'?>
<events>
 <event>
  <authorid>1</authorid>
  <url>http://garion.gwplus.net</url>
  <date>2008-01-13</date>
  <time>21:11:01</time>
  <description>a blog post</description>
  <type>video</type>
 </event>
</events>


And my PHP file:

Quote:
Code:
<?php
$xml = file_get_contents('getxml.php');
$xml = simplexml_load_string($xml);

for($i = 0; $i < count($xml->event); $i++){
  echo $xml->event[$i]->author. "<br />";
}
?>
Agent ME
Your PHP file is opening the PHP XML file and reading the file directly - the XML file is never parsed directly by PHP, so it's returning PHP code instead of XML.
Ranfaroth
Right.

Instead of file_get_contents('getxml.php'); you should use file_get_contents('http://path/to/your/getxml.php');
garionw
Thanks for the help you guys, 30frih each coming your way Wink

Quote:
Code:
 $lastauthor = 'fred';
 for($i = 0; $i < count($xml->event); $i++){
   if($lastauthor != $xml->event[$i]->author)echo 'no - ';
   echo $xml->event[$i]->author. " - ". $lastauthor. "<br />\n";
   $lastauthor = $xml->event[$i]->author;
 }


In the end I got it to work, but I can't get the above statement to work. This is the output:

Quote:
no - Garion - fred
no - Garion - Garion
no - bob - Garion
no - bob - bob


In a nutshell, I want it to see if there is a different author then last time. However, as you can see in the second line of the second quote, its just not happening. I'm thinking it has something to do with the for statement, but I'm not sure. Does anyone have any ideas?
Agent ME
Looking at the code, I'd guess that you're using the if statement wrong, and it is running the line "echo 'no - ';" at every run. Try putting the { } braces around it and seeing if that changes it. I'm not used to using if or similar commands without { } - it might be that your code is fine and the problem is somewhere else.

If its not that, then it has to be in the if statement. Instead of using
Code:
if($lastauthor != $xml->event[$i]->author)

try
Code:
if(strcasecmp($lastauthor, $xml->event[$i]->author)==0)

Just a wild guess - the code looks like it should work fine.

If neither of those work, could you post the part of the xml file its parsing, or the output of print_r($xml->event); ?
Related topics
What computer languages you know?
web tech tutorials(asp, php, xml, sql, svg ...)
AJAX?
Waarover gaan jullie sites?
i'm surprised.
how did you learn about html, php or other languages???
Shout box script
Quel language de programmation ?
Parse feed tags/urls using rss2html
Can it be possible?
PHP XML Parsing
Extracting XML with PHP
Explain what the Holy Spirit is???
PHP / XML Sitemap
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.