2014-12-27 75 views
0

我用下面的XML工作XML文化屬性嵌套在數據

<?xml version="1.0" encoding="UTF-8"?> 
<ns2:worldmate-parsing-result xmlns:ns2="http://www.worldmate.com/schemas/worldmate-api-v1.xsd" request-id="100793160" received="2014-12-27T16:34:42.000Z"> 
    <status>SUCCESS</status> 
    <error-msg>An itinerary confirmation e-mail was parsed successfully</error-msg> 
    <headers> 
     <header value="multipart/alternative; boundary=&quot;Apple-Mail=_62E5BF7A-C482-4FE0-8F43-15613E46D5FD&quot;" name="Content-type" /> 
     <header value="&lt;[email protected]&gt;" name="Return-Path" /> 
     <header value="rule=notspam policy=default score=0 spamscore=0 suspectscore=3 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1412080000 definitions=main-1412270180" name="X-Proofpoint-Spam-Details" /> 
    </headers> 
    <end-user-emails> 
     <user email="[email protected]" /> 
    </end-user-emails> 
    .... 
</ns2:worldmate-parsing-result> 

我想在XML中訪問以下數據:

[email protected] 
[email protected] 

我試圖做訪問第二電子郵件地址使用以下代碼。

$endus='end-user-emails'; 
$endUserEmails=$xml->$endus->attributes()->{'user'}; 
echo $endUserEmails; 

不知道爲什麼,但它迫使我使用變量名,如果我使用破折號,我會得到錯誤。

+0

問題是有效的XML標識符*不一定是有效的PHP標識符。如果它們包含破折號,正如您注意到的那樣,它們被視爲減法......並可能是語法錯誤。在這些情況下,你需要「可變」技巧。您可以使用XPath查詢切換到100%的XML解決方案。 – LSerni

回答

1

您可以使用XML DOM Parser查詢XML:

$doc = new DOMDocument; 
$doc->Load('file.xml'); 
$xpath = new DOMXPath($doc); 
$entries = $xpath->query('//end-user-emails/user/@email'); 
foreach ($entries as $entry) { 
    echo $entry->nodeValue; //Or do something else with the email value 
} 

關鍵的是查詢:

//end-user-emails/user/@email 

這意味着:「對於一個標籤<end-user-emails>下的每個標籤<user>,返回email屬性。」

對於第一電子郵件這可以被代替:

//headers/header/@value 

,然後取下&lt;&gt;

例子:(與php -a

$ PHP -a 交互模式啓用

php > $doc = new DOMDocument(); 
php > $xmldoc = '<?xml version="1.0" encoding="UTF-8"?> 
php ' <ns2:worldmate-parsing-result xmlns:ns2="http://www.worldmate.com/schemas/worldmate-api-v1.xsd" request-id="100793160" received="2014-12-27T16:34:42.000Z"> 
php '  <status>SUCCESS</status> 
php '  <error-msg>An itinerary confirmation e-mail was parsed successfully</error-msg> 
php '  <headers> 
php '   <header value="multipart/alternative; boundary=&quot;Apple-Mail=_62E5BF7A-C482-4FE0-8F43-15613E46D5FD&quot;" name="Content-type" /> 
php '   <header value="&lt;[email protected]&gt;" name="Return-Path" /> 
php '   <header value="rule=notspam policy=default score=0 spamscore=0 suspectscore=3 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1412080000 definitions=main-1412270180" name="X-Proofpoint-Spam-Details" /> 
php '  </headers> 
php '  <end-user-emails> 
php '   <user email="[email protected]" /> 
php '  </end-user-emails> 
php ' </ns2:worldmate-parsing-result>'; 
php > $doc->loadXML($xmldoc); 
php > $entries = $xpath->query('//end-user-emails/user/@email'); 
php > foreach ($entries as $entry) { 
php { echo $entry->nodeValue."\n"; 
php { } 
[email protected] 
0

利用簡單的XML(加載字符串或文件),您可以訪問的電子郵件:

$a = 'end-user-emails'; 
$xml = simplexml_load_string($str); 

echo ($xml->$a->user->attributes()->email); 

您需要指定值所在的標記,然後調用屬性的名稱