xml is externalFile.xml and has xml start tags

I had this question after viewing given xml, please extract a portion of xml line in output one per line.

Thanks. I have a related question where xml is externalFile.xml and has xml start tags
LVL 1
rgb192Asked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
By use the DOMDocument's load method as slightwv mentioned, here's an example.

1. let's say you got externalFile.xml with these content:

<smses>
<sms protocol="0" address="5551212" date="1500334066013" type="2" subject="null" body="I ONLY WANT BODY OF MESSAGE 1" 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 2" 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 3" 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 4" 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 5" 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 6" 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)" />
</smses>

Open in new window


2. apply codes like this:

<?php

$doc = new DOMDocument();
$file = "D:\\yourPath\\externalFile.xml";
$doc->load($file);

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

?>

Open in new window

0
 
slightwv (䄆 Netminder) Commented:
I find the documentation to be the best source for finding alternate methods.

In this case the code in the previous question used LoadXML.

Start with that doc link:
http://php.net/manual/en/domdocument.loadxml.php

Then way down at the bottom you see:
See Also
•DOMDocument::load() - Load XML from a file


That takes you to:
http://php.net/manual/en/domdocument.load.php
0
 
Ryan ChongCommented:
do you need further clarification here?

example above should work for you as well.
0
 
rgb192Author Commented:
this got the body tag

thanks
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.