0

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=&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>

I am trying to access the following data within the xml:

[email protected]
[email protected]

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.

1
  • The problem is that valid XML identifiers are not necessarily valid PHP identifiers. If they contain dashes, as you noticed, they are treated as subtractions... and probably syntax errors. In those cases you need the "variable" trick. You can switch to a 100% XML solution using XPath queries. Commented Dec 27, 2014 at 22:06

2 Answers 2

1

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 &lt; and &gt;

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=&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]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.