rgb192
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
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&desc=vacation_hawaii</loc>
<lastmod>2004-12-23</lastmod>
</url>
<url>
<loc>http://www.example.com/catalog?item=73&desc=vacation_new_zealand</loc>
<lastmod>2004-12-23</lastmod>
</url>
<url>
<loc>http://www.example.com/catalog?item=74&desc=vacation_newfoundland</loc>
<lastmod>2004-12-23T18:00:15+00:00</lastmod>
</url>
<url>
<loc>http://www.example.com/catalog?item=83&desc=vacation_usa</loc>
<lastmod>2004-11-23</lastmod>
</url>
</urlset>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ray for this question
file.xml is the input
I want to find out the urls already in file.xml
file.xml is the input
I want to find out the urls already in file.xml
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think simplexml is easier to understand and extend
but the regex works also
<?php
$xml=file_get_contents('fi le.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.
but the regex works also
<?php
$xml=file_get_contents('fi
// 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.
http://www.laprbass.com/RAY_temp_rgb192.php
Open in new window
HTH, ~Ray