Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用SimpleXML获取属性和值_Php_Attributes_Simplexml - Fatal编程技术网

Php 使用SimpleXML获取属性和值

Php 使用SimpleXML获取属性和值,php,attributes,simplexml,Php,Attributes,Simplexml,我真的不明白如何在PHP中使用SimpleXML 下面是我的XML文件的一个示例: <?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 detecte

我真的不明白如何在PHP中使用SimpleXML

下面是我的XML文件的一个示例:

<?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>

检测到网络访问
允许
程序c:\xampp\apache\bin\httpd.exe试图连接到Internet。程序在端口80上使用TCP协议。
我需要检索以下内容: 来源、时间戳、主题、操作、消息

我就是不明白。有人能帮我吗?

为了教你钓鱼,我鼓励你去看看这个网站

帮助你开始工作

  • 使用
    simplexml\u load\u file()
    simplexml\u load\u string()
    解析XML
  • 这将返回一个对象-使用
    var\u dump()
    print\r()
    查看它的外观
  • 遍历此对象以获得所需的属性
  • 此代码应适用于:

    $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;
    
    。。。其中,$xmlString是xml文件的字符串

    了解如何使用simpleXML

    希望这对你有帮助,祝你好运

    尝试以下操作:

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

    哇!我没想过!谢谢但是我有多个'event'标记,:S所以我只是把它放在foreach循环之间?如果你想在所有事件标记下获得所有的主题、动作和消息,那么是的,只需要使用foreach循环。如果您只想获得一个特定的事件,请使用事件[0]、事件[1]等。很高兴我提供了帮助!是的,谢谢,我已经看过这些文件了。。。但我会尝试使用var_dump,通过多次尝试,我应该能够得到我想要的。谢谢:)问题就是问题,答案就是答案。我将您的编辑移到下面的答案中:-您也可以这样做(通过访问问题的历史记录),如果您愿意,我将删除我的副本。