我正在使用以下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="Apple-Mail=_62E5BF7A-C482-4FE0-8F43-15613E46D5FD"" name="Content-type" />
<header value="<xxxxx@me.com>" 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="yyyyy@me.com" />
</end-user-emails>
....
</ns2:worldmate-parsing-result>
我正在尝试访问xml中的以下数据:
xxxxx@me.com
yyyyy@me.com
我正在尝试使用以下代码访问第二个电子邮件地址。
$endus='end-user-emails';
$endUserEmails=$xml->$endus->attributes()->{'user'};
echo $endUserEmails;
不确定为什么,但它强迫我使用变量名,如果我使用破折号,我会收到错误。
答案 0 :(得分: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
这意味着:“对于标记<user>
下的每个代码<end-user-emails>
,都会返回email
属性。”
对于第一封电子邮件,可以将其替换为:
//headers/header/@value
然后删除<
和>
;
示例:(使用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="Apple-Mail=_62E5BF7A-C482-4FE0-8F43-15613E46D5FD"" name="Content-type" />
php ' <header value="<xxxxx@me.com>" 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="yyyyy@me.com" />
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 { }
yyyyy@me.com
答案 1 :(得分:0)
使用简单的xml(加载字符串或文件),您可以使用以下方式访问电子邮件:
$a = 'end-user-emails';
$xml = simplexml_load_string($str);
echo ($xml->$a->user->attributes()->email);
您需要指定值所在的标记,然后调用属性的名称