given xml, please extract a portion of xml line in output one per line

I want only the body of this xml
each line on a separate line


  <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />

Open in new window


output expected:
I ONLY WANT BODY OF MESSAGE
I ONLY WANT BODY OF MESSAGE
I ONLY WANT BODY OF MESSAGE
I ONLY WANT BODY OF MESSAGE
I ONLY WANT BODY OF MESSAGE
I ONLY WANT BODY OF MESSAGE
I ONLY WANT BODY OF MESSAGE
LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try this:

<?php

$xml = '<root><sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" /> <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" />
 <sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Jul 17, 2017 7:27:46 PM" contact_name="(Unknown)" /></root>';

$doc = new DOMDocument();
$doc->loadXML($xml);

$smses = $doc->getElementsByTagName('sms');
foreach ($smses as $sms) {
    $body = $sms->getAttribute('body');
    echo $body."<br>";
}

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Thanks. I have a related question where xml is externalFile.xml and has xml start tags
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.