want all the <url></url> nodes in an xml file using php

using php create code to parse this external file.xml as input
creating output

http://www.example.com/
http://www.example.com/catalog?item=12&desc=vacation_hawaii
http://www.example.com/catalog?item=73&desc=vacation_new_zealand
http://www.example.com/catalog?item=74&desc=vacation_newfoundland
http://www.example.com/catalog?item=83&desc=vacation_usa

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

   <url>

      <loc>http://www.example.com/</loc>

      <lastmod>2005-01-01</lastmod>

   </url>

   <url>

      <loc>http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii</loc>

      <lastmod>2004-12-23</lastmod>

   </url>

   <url>

      <loc>http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand</loc>

      <lastmod>2004-12-23</lastmod>

   </url>

   <url>

      <loc>http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland</loc>

      <lastmod>2004-12-23T18:00:15+00:00</lastmod>

   </url>

   <url>

      <loc>http://www.example.com/catalog?item=83&amp;desc=vacation_usa</loc>

      <lastmod>2004-11-23</lastmod>

   </url>

</urlset>

Open in new window

LVL 1
rgb192Asked:
Who is Participating?
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.

Marco GasiFreelancerCommented:
This should work fine:

$str = file_get_contents('file.xml');
$regex = '#(?<=\<loc>)[^<]*(?=\</loc>)#';
preg_match_all($regex, $str, $natches);
var_dump($matches[0]);

Open in new window

0
Ray PaseurCommented:
As usual, the context is at least as important as the content.
http://www.laprbass.com/RAY_temp_rgb192.php

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

/** http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28271756.html
 * See also:
 * http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28272016.html
with the php input an array of urls
http://www.example.com/
http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii
http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand
http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland
http://www.example.com/catalog?item=83&amp;desc=vacation_usa
 */

$arr = array
( 'http://www.example.com/'
, 'http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii'
, 'http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand'
, 'http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland'
, 'http://www.example.com/catalog?item=83&amp;desc=vacation_usa'
)
;

$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
EOD;

foreach ($arr as $url)
{
    $xml
    .= PHP_EOL
    . '<url>' . PHP_EOL
    . '<loc>' . $url . '</loc>' . PHP_EOL
    . '<lastmod>' . date('Y-m-d') . '</lastmod>' . PHP_EOL
    . '</url>'
    ;
}
$xml .= PHP_EOL . '</urlset>';

// TEST: IS IT A VALID XML DOCUMENT?
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SHOW THE WORK PRODUCT
// echo htmlentities($xml);

// EXTRACT THE URLs FROM THE XML DOCUMENT
foreach($obj as $url)
{
    echo PHP_EOL . $url->loc;
}

Open in new window

HTH, ~Ray
0
rgb192Author Commented:
Ray for this question
file.xml is the input

I want to find out the urls already in file.xml
0
Ray PaseurCommented:
Read file.xml with file_get_contents('file.xml');

The run line 42 through the end of the code I posted above.  If that doesn't do what you want, please post the input data along with something that shows what you want to get for output and I'll be glad to try to help.
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:
I think simplexml is easier to understand and extend
but the regex works also


<?php
$xml=file_get_contents('file.xml');
// TEST: IS IT A VALID XML DOCUMENT?
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SHOW THE WORK PRODUCT
// echo htmlentities($xml);

// EXTRACT THE URLs FROM THE XML DOCUMENT
foreach($obj as $url)
{
    echo PHP_EOL . $url->loc;
}



thank you.
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.