1

Hey guys, I have looked all around for help but found nothing sadly. I am trying to use PHP to grab the contents of an XML file and pull the data from an attribute. I have checked other tutorials and tried but none worked.

Here is the XML file:

<eveapi version="2">
  <currentTime>2011-04-03 03:55:59</currentTime>
  <result>
    <rowset name="notifications" key="notificationID" columns="notificationID,typeID,senderID,sentDate,read">
      <row notificationID="339040500" typeID="75" senderID="1000137" sentDate="2011-04-03 03:53:00" read="0" />
    </rowset>
  </result>
  <cachedUntil>2011-04-03 04:25:59</cachedUntil>
</eveapi>

I am trying to pull the data from 'typeID' & 'sentDate'. Also, this XML file may containt multiple tags.

All help is welcomed, thanks!

5
  • Have you tried simplexml? (php.net/simple_xml) Commented Apr 4, 2011 at 5:02
  • @Ben It's 2011, I sure hope he's using PHP 5 ;-) Commented Apr 4, 2011 at 5:08
  • i have an answer but its a little unorthodox, and dont wanna risk it :/ Commented Apr 4, 2011 at 5:16
  • @Pascal. Can't assume. Honestly, the things you see in production environments.. Commented Apr 4, 2011 at 5:24
  • Yeah, I know :-( But, still... Commented Apr 4, 2011 at 5:35

2 Answers 2

2

You can use the following code :-

$xmlstr = '<eveapi version="2">
  <currentTime>2011-04-03 03:55:59</currentTime>
  <result>
    <rowset name="notifications" key="notificationID" columns="notificationID,typeID,senderID,sentDate,read">
      <row notificationID="339040500" typeID="75" senderID="1000137" sentDate="2011-04-03 03:53:00" read="0" />

    </rowset>
  </result>
  <cachedUntil>2011-04-03 04:25:59</cachedUntil>
</eveapi>
';


$xml = simplexml_load_string($xmlstr);

print_r ($xml->result->rowset->row['typeID']);

Note: If you have multiple row in rowset then in object, the row will be as collection of array. In that case you have to access the typeID like bellow -

print_r ($xml->result->rowset->row[0]['typeID']);
Sign up to request clarification or add additional context in comments.

1 Comment

In addition, Don't worry about the value as showing SimpleXMLElement Object. You can get just the value when you will print/echo the variable.
1

Putting flame retardant suit on...

$string = '<eveapi version="2">
  <currentTime>2011-04-03 03:55:59</currentTime>
  <result>
    <rowset name="notifications" key="notificationID" columns="notificationID,typeID,senderID,sentDate,read">
      <row notificationID="339040500" typeID="75" senderID="1000137" sentDate="2011-04-03 03:53:00" read="0" />
    </rowset>
  </result>
  <cachedUntil>2011-04-03 04:25:59</cachedUntil>
</eveapi>';

preg_match_all('#([\S]+)="(.*?)"#is', $string, $matches);
unset($matches[0]);

$xml = array_combine($matches[1], $matches[2]);

print_r($xml);

=

Array
(
    [version] => 2
    [name] => notifications
    [key] => notificationID
    [columns] => notificationID,typeID,senderID,sentDate,read
    [notificationID] => 339040500
    [typeID] => 75
    [senderID] => 1000137
    [sentDate] => 2011-04-03 03:53:00
    [read] => 0
)

2 Comments

nice, hold head in shame 'n' quickly deletes his lame answer
Knowing the eveapi I think that this answer is irresponsible. @Narg will probably want to do more with XML and a quick n dirty regex isn't the way to go and doesn't teach anyone a thing about how to properly handle XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.