Get Attributes and Values ​​Using SimpleXML

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

Here is an example 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 get this: Source, Timestamp, Subject, Action, Message

I just do not understand. Can someone please help me with this?

+6
source share
3 answers

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 line of the xml file.

Read how to use simpleXML here .

Hope this helped and good luck!

+16
source

In the interest of teaching you how to fish, I would advise you check out PHP Docs on SimpleXML .

To help you get started, though.

  • Use simplexml_load_file() or simplexml_load_string() to parse XML
  • This will return the object - use var_dump() or print_r() to see what it looks like.
  • Go through this object to get the attributes you need.
+1
source

Try the following:

 function time2DatetimeUS($timestamp) { $datetime = date('Ymd 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"; 
+1
source

Source: https://habr.com/ru/post/890835/


All Articles