开发者

Get attributes and values using SimpleXML

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开发者_C百科. Can someone please help me with this?


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!


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.


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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜