我正在使用以下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;不知道为什么,但这迫使我使用变量名,如果我使用破折号,我会得到错误。
发布于 2014-12-27 21:55:43
您可以使用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>__下的每个标记__,返回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发布于 2014-12-27 22:00:30
使用简单的xml (加载字符串或文件),您可以通过以下方式访问电子邮件:
$a = 'end-user-emails';
$xml = simplexml_load_string($str);
echo ($xml->$a->user->attributes()->email);您需要指定值所在的标记,然后调用属性的名称
https://stackoverflow.com/questions/27671586
复制相似问题