I am working with the following 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>
I am trying to access the following data within the xml:
xxxxx@me.com
yyyyy@me.com
I am trying to do access the second email address using the following code.
$endus='end-user-emails';
$endUserEmails=$xml->$endus->attributes()->{'user'};
echo $endUserEmails;
Not sure why but it is forcing me to use a variable name, and if I use dashes I get errors.
You could use the XML DOM Parser
to query the 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
}
Crucial is the query:
//end-user-emails/user/@email
Which means: "For every tag <user>
under a tag <end-user-emails>
, return the email
attribute."
For the first email this can be replaced by:
//headers/header/@value
and then remove the <
and >
;
Example: (with php -a
)
$ php -a Interactive mode enabled
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."
";
php { }
yyyyy@me.com
Using simple xml (load string or file), you can access the emails with:
$a = 'end-user-emails';
$xml = simplexml_load_string($str);
echo ($xml->$a->user->attributes()->email);
You need to specify the tag the value is in, and then call the attribute's name