These forums have been archived and are now read-only.

The new forums are live and can be found at https://forums.eveonline.com/

EVE Technology Lab

 
  • Topic is locked indefinitely.
 

Php and XML displaying Help

Author
Louis Vitton
Viziam
Amarr Empire
#1 - 2011-10-03 06:06:30 UTC
Hey guys,

I am having issues trying to parse some of the XML from https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID=90926985

Quote:

rowset name="employmentHistory" key="recordID" columns="recordID,corporationID,startDate" row recordID="17532403" corporationID="98052700" startDate="2011-08-01 23:47:00" row recordID="17204306" corporationID="1000170" startDate="2011-06-26 15:46:00"

I have removed some of the formatting from here as it would not let me place HTML formatting in this forum.


This section i can not manage to parse, i am using simple xml the same as this code below shows

Quote:

< ?php

$data = file_get_contents('https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID=90926985');
$xml = new SimpleXMLElement($data);
#var_dump($xml);


print $xml->result->{'characterName'}->{0};


? >


Any help you could give me i would be very happy about. Thank you.

Cheers Louis
Lutz Major
Austriae Est Imperare Orbi Universo
#2 - 2011-10-03 06:54:11 UTC
Statement: I don't have any clue regarding PHP, but

guessing: when you can access the node element 'result' by
Quote:
print $xml->result;
why can't you access the element 'characterName' with
Quote:
print $xml->result->characterName;
?

Also, can it be, that {0} is an accessor for an array? Which clearly the characterName is not? (or does (String){0} return the first character - in your case 'S'?)
Louis Vitton
Viziam
Amarr Empire
#3 - 2011-10-03 07:58:02 UTC
I can print the charater name i can not print the employment history section
Lutz Major
Austriae Est Imperare Orbi Universo
#4 - 2011-10-03 08:20:55 UTC
Louis Vitton wrote:
I can print the charater name i can not print the employment history section

Apologetically: Sorry, I then misinterpreted your request.

Does
Quote:
$xml->result->row[0]
not work?

or something like
Quote:

foreach ($xml->result->row as $row) {
print $row->corporationID
}
Louis Vitton
Viziam
Amarr Empire
#5 - 2011-10-03 08:40:13 UTC
No neither seem to work :(
Lutz Major
Austriae Est Imperare Orbi Universo
#6 - 2011-10-03 10:03:09 UTC
Thanks to my colleague, who knows I'm incapable of PHP, it's
Quote:
print $xml->result->rowset->row[0]{'corporationID'};
or
Quote:
foreach ($xml->result->rowset->row as $row) {
print $row{'recordID'};
}


Big smile
Louis Vitton
Viziam
Amarr Empire
#7 - 2011-10-03 15:09:20 UTC
Lutz Major wrote:
Thanks to my colleague, who knows I'm incapable of PHP, it's
Quote:
print $xml->result->rowset->row[0]{'corporationID'};
or
Quote:
foreach ($xml->result->rowset->row as $row) {
print $row{'recordID'};
}


Big smile


Thank you mate it works now.

I am a complete noob to php and this has been a huge help. :)
Kossaw
Body Count Inc.
#8 - 2011-10-06 07:43:47 UTC  |  Edited by: Kossaw
when you parse XML elements the value often comes back as an array datatype- which cant be printed.

You probably need to force the dataype using (string), (int), (float) etc. For example ......

print (string)$xml->result->rowset->row[0]->characterName;
print (int)$xml->result->characterID;

the reason that parsing with foreach($row as $x) works is that $x doesn't come back as an array

WTB : An image in my signature

TorTorden
Tors shibari party
No Holes Barred
#9 - 2011-10-06 23:56:17 UTC
at least with php and simplexml assumes all values are strings.
wich is of course fine for echoing, but if you need to do some math with it you must as mentioned type cast them like $x=(double) $xml->something->something...

fenistil
Garoun Investment Bank
Gallente Federation
#10 - 2011-10-09 13:29:08 UTC
A Net trick I figured for SimpleXML is this:

Quote:

$sXmlObject = simplexml_load_string($xml);
$xmlObject = json_decode(json_encode($sXmlObject));


Now all SimpleXMLElement Objects are converted to StdClass objects. Might be easier to use and certainly easier to serialize and store in DB.

.

Dragonaire
Here there be Dragons
#11 - 2011-10-09 15:43:57 UTC
If your going to be storing it in the database in an unprocessed form why not just store the XML directly then you don't have to serialize etc.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Louis Vitton
Viziam
Amarr Empire
#12 - 2011-10-09 23:19:41 UTC
Dragonaire wrote:
If your going to be storing it in the database in an unprocessed form why not just store the XML directly then you don't have to serialize etc.


Hey Dragonaire,

Do you have any examples of how to do this that i can read up on?

I was going to use your Yapeal coding but there is a lot there i just do not understand yet as i am teaching myself mySql and php as i am building things and going forward slowly.
I am an IT student but i study in IT networking not programming but have a real interest in this area for side projects and hobby work like building this API tool I want.

Cheers,

Louis
Dragonaire
Here there be Dragons
#13 - 2011-10-10 01:07:15 UTC
In Yapeal I'm caching the XML in the private method cacheXmlDatabase() which is in class/YapealApiCache.php but it's not complex once you have the XML as a string to pass to over to SimpleXML just do a insert to the database. All you need is a simple table like this:

CREATE TABLE IF NOT EXISTS `utilXmlCache` (
`hash` char(40) CHARACTER SET ascii NOT NULL,
`api` char(32) CHARACTER SET ascii NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`section` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`xml` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`hash`),
KEY `utilXmlCache1` (`section`),
KEY `utilXmlCache2` (`api`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

which is directly from Yapeal's DB. The hash I use as primary key is formed from the parameters use to get the XML from CCP server plus some other stuff. You could use something else or even not have it if you're just making a single character test application.

SimpleXML also has methods to return stuff as string or save to file if you prefer. Best places to learn how to do this stuff is the same places I learned from which are the PHP manual online and maybe a few APress or O'Reilly book on using PHP SimpleXML, and MySQL. Most schools give IT students access to a collection of them online for free.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.