Jump to content

XML Parsing from external URL

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Ms_Kristina

Ms_Kristina

    Newbie

  • Members
  • Pip
  • 8 posts
A friend of mine wrote up some code before to parse XML which he passed onto me as an example. I modified for my purposes however it does not seem to be working.

I added test lines to get an idea where in the code it stops, and the online printed line I get is "connected".

Any thoughts on what the problem could be?

Code:

<?php


     // Connect to the MySQL database

   $conn = mysql_connect("localhost", "root", "XXXXX");


   // Select the database

   $db = mysql_select_db("royalities");


   // Query the table

   $query = "SELECT * FROM roster";

   $result = mysql_query($query) or die(mysql_error());

   echo "connected";


   // Loop through each row, submit HTTP request, output coordinates

   while (list($cname, $url) = mysql_fetch_row($result))

   {

   	echo "in while loop";

      

      $url = "PM for the $URL";


      // Retrieve the URL contents

      $page = file_get_contents($url);

      echo "getting url contents";


      // Parse the returned XML file

      $xml = new SimpleXMLElement($page);

      echo "getting xml";


      // Parse the coordinate string

        list($cname) = $xml->guildKey->guildInfo->guild->members->character[7]->attributes();

        list($url) = $xml->guildKey->guildInfo->guild->members->character[11]->attributes();

        echo "parsing";


		$charsheeturl = "PM for CHAR SHEET URL";



	// Output to table

	//$query2 = "UPDATE roster SET name = '$cname', url = '$url' WHERE id = '$id'";

	$InsertQuery = "INSERT INTO roster (name, url) VALUES ('$name', '$url')";

	echo "insert query $InsertQuery";


	$RunInsertQuery = mysql_query($InsertQuery) or die(mysql_error(error));

	echo "run query";


	// Output to Screen

	echo "Name: $cname";

	echo "URL: $charsheeturl$url";

   }


?>




#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
As nothing prints out after "connected", it has to be the while statement that never becomes true. Are you sure that the table "roster" has rows and it only has two fields that you wnat to rad out (as your sql statement wants all fields)
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Ms_Kristina

Ms_Kristina

    Newbie

  • Members
  • Pip
  • 8 posts
Seems I may need to add an if/else here.

As, right now my table is blank, and needs to be populated by the info for the first time.

Once the table is populated, it will have info in the table that will need to verify against the WoW Armory XML guild list to first get char name and url.
- If name isn't there it needs to add it from the table
- If name is there it needs to update it from the table
- If name is not there it needs to delete it from the table.

Meep

Any thoughts on what the if/else should look like?

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I think it's better to just loop the XML each time, and update your table depending on contents.. is there a single page/XML-document where you can get all members of the guild?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
Ms_Kristina

Ms_Kristina

    Newbie

  • Members
  • Pip
  • 8 posts
No, they have a guild info one which is a basic listing of the members, and then the char sheets which is one for each character and lots of info for each char -- my GM wants the detailed info. >_<

So I have no choice but get the basic listing, and then get each char.

I re-wrote the code but the site won't let me post it because it keeps saying there are links in it where there are none Grar.

Edited by Ms_Kristina, 06 April 2009 - 02:32 AM.
Adding more info.


#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Yes, that's the way you need to do it, loop through the basic info and for each, get the details.

There are two ways of deleting those who isn't there any longer; the first one is to truncate the table from start, and then add all as new. the second way is to add a date field that changes upon update, and when the script is done, you delete all rows that doesn't have the current date.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall