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?
 
Ryan ChongCommented:
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
 
rgb192Author Commented:
Thanks. I have a related question where xml is externalFile.xml and has xml start tags
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.