php 使用 SimpleXML 获取属性和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6399504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:15:10  来源:igfitidea点击:

Get attributes and values using SimpleXML

phpattributessimplexml

提问by Jeremy Dicaire

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

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

Here is an exemple of my XML file:

这是我的 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>

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

我需要检索这个:源、时间戳、主题、操作、消息

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

我只是不明白。有人可以帮我解决这个问题吗?

回答by Jeff Gortmaker

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.

... 其中 $xmlString 是 xml 文件的字符串。

Read up on how to use simpleXML here.

此处阅读有关如何使用 simpleXML 的信息

Hope this helped and good luck!

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

回答by Jason McCreary

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

为了教您钓鱼,我鼓励您查看SimpleXML 上PHP 文档

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. 使用simplexml_load_file()simplexml_load_string()解析您的 XML
  2. 这将返回一个对象 - 使用var_dump()print_r()查看它的外观。
  3. 遍历这个对象来获取你想要的属性。

回答by hakre

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