我想用PHP來處理幾個XML文件。我已經通讀了php simpleXML網站上的解釋和一些示例,但我無法從xml中獲得我想要的內容。
我無法控制xml。下面是XML文件的一個片段:麻煩與PHP和XML
<end-user-emails>
<user email="[email protected]"/>
</end-user-emails>
的代碼片段我目前有:
$result = $xml->xpath("end-user-emails/user[@email]");
print_r($result[0][email]);
,輸出:
SimpleXMLElement Object ([0] => [email protected])
我無法找到一個方法來簡單地返回屬性值。
我已經嘗試將其轉換爲字符串並獲取錯誤。 我已經試過幾個變化:
$result = $xml->end-user-emails[0]->user[0]->attributes();
,它告訴我,儘管前面的輸出,我不能叫屬性(),因爲它不會被調用的對象上。因此,如果任何人都可以讓我知道如何從XML中獲取屬性名稱和值,那將是非常感謝。屬性名稱不nessasary,但我想用它,所以我可以確認我其實抓住電子郵件,是這樣的:
if attributeName = "email" then $email = attributevalue
爲什麼不使用'$ result [0] ['email'] [0]'? – 2013-04-03 19:56:56
試過了,輸出沒變。 – 2013-04-03 20:00:26
不確定,並且可能完全不相關,但在XML元素名稱中允許使用破折號? **更新**沒關係,破折號是允許的,而不是第一個字符。 – thaJeztah 2013-04-03 20:06:08