使用SimpleXML获取属性和值

Jer*_*ire 7 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 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>
Run Code Online (Sandbox Code Playgroud)

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

我只是不明白.有人可以帮我这个吗?

Jef*_*ker 17

这段代码应该有效:

$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;
Run Code Online (Sandbox Code Playgroud)

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

阅读有关如何在这里使用simpleXML的信息.

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