Pages: [1] :: one page |
|
Author |
Thread Statistics | Show CCP posts - 0 post(s) |
Harcole
Amarr Sanguine Unity
|
Posted - 2009.08.13 14:22:00 -
[1]
I'm trying my hand at a PHP API reader for a little project I am making, I've been using PHP a good few years but never worked with XML, the concept I understand and I like. However the script I came up with (thanks to help from Google) has left me with a minor issue. It only reads one tag deep so I end up with this:
Current State: WILLPOWER Current State: ROWSET Current State: ROWSET Current State: ROWSET Current State: ROWSET Current State: ROWSET Current State: ROWSET Current State: CACHEDUNTIL
How do I get at the ROWS behind those ROWSET's and know where they came from, my rather basic code is this:
<?php
# Example of using PHP and XML to parse a file # Tutorial followed from http://www.codehelp.co.uk/php/xmlparse1.php
# attempt to open the XML file if(!($fp = @fopen("the eve address to my API was here!", "r"))) die ("Couldn't open XML file.");
# Setup variables # userCount keep track of number Contacts found $userCount= 0; # userData will hold the data of each user $userData= array(); # state keeps track of which node the parser is dealing with $state= '';
# attempt to create the parser if(!($xml_parser = xml_parser_create())) die ("Could not create XML Parser.");
# Create a function to detect the start of an element within the XML file function startElementHandler ($parser, $name, $attrib){ # Make sure all variables can be used from outside function global $userCount; global $userData; global $state;
switch($name){ case $name == "ROWSET" :{ $userData[$userCount]["rowsetName"] = $atrrib["NAME"]; }
default :{ $state = $name; break; } } }
# This function watches for the end of an element function endElementHandler ($parser, $name){ # Make sure all variables can be used from outside function global $userCount; global $userData; global $state;
$state = ''; if($name == "RESULT"){ $userCount++; } }
# This function handles the character data function characterDataHandler ($parser, $data){ # Make sure all variables can be used from outside function global $userCount; global $userData; global $state;
if(!$state){return;} echo "Current State: ".$state."<br />"; if($state == "NAME"){$userData[$userCount]["name"] = $data;} if($state == "RACE"){$userData[$userCount]["race"] = $data;} if($state == "CORPORATIONNAME"){$userData[$userCount]["corporationName"] = $data;} if($state == "BALANCE") {$userData[$userCount]["walletBalance"] = $data;} }
# now we tell the parser which functions to call, read the data from the opened file and parse the contents xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler"); xml_set_character_data_handler($xml_parser, "characterDataHandler");
# break the file up if its bigger than 4MB in size while($data = fread($fp, 4096)){ if(!xml_parse($xml_parser, $data, feof($fp))) { break; } } xml_parser_free($xml_parser);
print_r($userData); echo "<hr /><br />"; # The data by this point is held in $userData and is accessed using the loop below for($i=0; $i<$userCount; $i++){ echo "EvE API Results: <br />"; echo $userData[$i]["name"]." ".$userData[$i]["race"]."<br />".$userData[$i]["walletBalance"]."<br />".$userData[$i]["corporationName"]; }
?>
I know this isn't tidy and Im sure I could of used one of the API library but that wouldn't of helped me to learn anything! I just can't find a way of getting to the ROW data hidden behind ROWSET.
Cheers Harc
|
Bloemkoolsaus
Divine Power. Wildly Inappropriate.
|
Posted - 2009.08.13 14:45:00 -
[2]
Your code is far more complicated then i should be...
To parse xml, I always use simplexml extension. It's usually available in apache by default.
Basicly, simplexml translates the xml to an object, wich you can read like an array.
if ($xml = new SimpleXMLElement($the_content_of_xml_file)) { // succes, do more stuff } else { // fail }
To read an element from your xml, you go like: $element = $xml->element; To read an attribute from that element, you can now do $element['attributename'];
You can loop through rowsets and rows
foreach ($xml->rowset->row as $row) { }
Once you get the hang of it, it's really simple.. Also, to help understand it it helps to print out the simplexml object.
print_r($xml);
Here are some links: http://us2.php.net/simplexml http://www.webmonkey.com/tutorial/Get_Started_With_SimpleXML_in_PHP
I'm not sure if my way is the "correct" way, if there is even a correct way.. Even though, I hope it helps
|
Dragonaire
Caldari Corax. New Eden Retail Federation
|
Posted - 2009.08.13 14:45:00 -
[3]
I'd use SimpleXML or even DOM and learn how to use XPath as then you can get the <row/>s with something like $rows=$api->xpath('//row'); which returns you a nice array of them to work with . Yes you do need to have PHP5 but since PHP4 is no longer being worked on or supported all the host sites have now finally moved up also. I personal like to use SimpleXML because it is just that simple but there are time when you might need DOM as well but there's functions to convert between them as needed. -- Finds camping stations from the inside much easier. Designer of Yapeal for Eve API.
|
Harcole
Amarr Sanguine Unity
|
Posted - 2009.08.13 14:50:00 -
[4]
thanks guys I'll go away, head hung in overly complicated shame!
I'll see how I get on with simpleXML thank you for links and examples!
|
Dragonaire
Caldari Corax. New Eden Retail Federation
|
Posted - 2009.08.13 14:58:00 -
[5]
I'd also point you at the code in Yapeal or any of the other PHP Api libraries for examples if you get stuck on anything as they have solve the problem already but I understand you're use this to learn more about XML mostly not just to use other people's code -- Finds camping stations from the inside much easier. Designer of Yapeal for Eve API.
|
Valek Rennesist
Immortalis Silens Slightly Inappropriate.
|
Posted - 2009.08.13 17:30:00 -
[6]
I'm probably the extra droning on of a broken record at this point, but in my experience, SimpleXML is far easier to use than XMLParser. I think you'll like it a lot better.
|
Harcole
Amarr Sanguine Unity
|
Posted - 2009.08.14 07:31:00 -
[7]
Thanks guys for the help, I've not looked at the other API yet (I'll go find them in a second and see if I can't work out what I'm doing wrong, but in the mean time!
When trying to access things I understand to use (in really basic terms):
print $xmlObject->result->name;
which would give in my case:
Harcole
But when trying to access those pesky ROWSET data I used:
print $xmlObject->result->rowset[0]->row[0]->typeID;
But the output for that is empty, if I use:
print_r $xmlObject->result->rowset[0];
I do get the array, so any clues as to how to access single items inside the rowset?
Thanks again...
Harc
|
Harcole
Amarr Sanguine Unity
|
Posted - 2009.08.14 07:36:00 -
[8]
Its amazing what that first cup of coffee does in a morning!
To correct myself its simple!
print $xmlObject->result->rowset[0]->row[0][typeID];
Sorry!!
|
|
|
|
Pages: [1] :: one page |
First page | Previous page | Next page | Last page |