7

I really don't understand how to use SimpleXML in PHP.

Here is an exemple of my XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<eventlog version="1.1">

<event source="Firewall" timeStamp="1308433939" type="0" deleted="0" bodyLength="218">
<subject>Network access detected</subject>
<action>Allowed</action>
<message>The program c:\xampp\apache\bin\httpd.exe attempted to connect to the Internet. The program used the protocol TCP on port 80.</message>
</event>

</eventlog>

I need to retrieve this: Source, Timestamp, Subject, Action, Message

I just don't get it. Can someone please help me with this?

1
  • Question is question and answer is answer. I moved your edit into an answer below: stackoverflow.com/a/14194288/367456 - You can do the same (from visiting the history of your question) if you like, I will then remove my copy.
    – hakre
    Jan 7, 2013 at 10:45

3 Answers 3

17

This code should work:

$xml = new SimpleXMLElement($xmlString);
$source = $xml->event->attributes()->source;
$timestamp = $xml->event->attributes()->timestamp;
$subject = $xml->event->subject;
$action = $xml->event->action;
$message = $xml->event->message;

... where $xmlString is the string of the xml file.

Read up on how to use simpleXML here.

Hope this helped and good luck!

2
  • Woah! I didn't think about that! Thanks! But i have multiple 'event' tag, :S so i just put it between foreach loop? Jun 18, 2011 at 23:34
  • 1
    If you want to get all of the subject, action, and messages under all of the event tags, then yea, just use a foreach loop. If you just want to get a specific one of the events, use event[0], event[1], etc. Glad I helped! Jun 18, 2011 at 23:36
1

In the interest of teaching you to fish, I'd encourage you to check out the PHP Docs on SimpleXML.

To help get your started though.

  1. Use simplexml_load_file() or simplexml_load_string() to parse your XML
  2. This will return an object - use var_dump() or print_r() to see what it looks like.
  3. Traverse this object to obtain the attributes you want.
1
  • Yep thanks, I already read this docs... But I'll try using var_dump and with multiple try i should be able to get what I want. Thanks :) Jun 18, 2011 at 23:35
1

Try the following:

function time2DatetimeUS($timestamp)
{
  $datetime = date('Y-m-d H:i:s', $timestamp);
  return $datetime;
}

$db = new SQLiteDatabase("AutoAnalysis.sqlite", 0666, $err);

$xml = new SimpleXMLElement($logs_antivirus_local, NULL, TRUE);
foreach ($xml->event as $a) {
    $source    = $a->attributes()->source;
    $timestamp = $a->attributes()->timeStamp;
    $datetime  = time2DatetimeUS("$timestamp");
    $subject   = $a->subject;
    $action    = $a->action;
    $message   = $a->message;
}

$query = "INSERT INTO BitDefender(id, datetime, module, sujet, action, message) 
                VALUES ('', '$datetime', '$source', '$subject', '$action', '$message')";
$results = $db->queryexec($query);
echo "     $datetime     $source  $subject";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.