Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

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

SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of rgb192

ASKER

Ray for this question
file.xml is the input

I want to find out the urls already in file.xml
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

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.