You can try this:
<pre><?php
$dom = new DOMDocument();
@$dom->load('Remotefile.xml');
$urls = $dom->getElementsByTagName('url');
foreach ($urls as $url) {
$image = $url->getElementsByTagName('image')->item(0);
$imageChildren = $image->childNodes;
$result[] = array( 'loc' => $url->getElementsByTagName('loc')->item(0)->textContent,
'imgloc' => $imageChildren->item(0)->textContent,
'imgcap' => $imageChildren->item(1)->textContent);
}
$stmt = $dbh->prepare ("INSERT INTO urls (loc, imageloc, imagecap) VALUES (:loc, :imgloc, :imgcap)");
foreach ($result as $res) {
$stmt -> bindParam(':loc', $res['loc']);
$stmt -> bindParam(':imgloc', $res['imgloc']);
$stmt -> bindParam(':imgcap', $res['imgcap']);
$stmt -> execute();
}
A regex way:
$pattern = <<<'LOD'
~
<url> \s*+
<loc> (?<loc> [^<]++ ) </loc> \s*+
<image:image> \s*+
<image:loc> (?<imgloc> [^<]++ ) </image:loc> \s*+
<image:caption> (?<imgcap> [^<]++ ) </image:caption> \s*+
</image:image> \s*+
</url>
~x
LOD;
preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);
/* this foreach part is only for cosmetic and is totally useless */
foreach($matches as &$match) {
foreach($match as $k=>$m) {
if (is_numeric($k)) unset($match[$k]);
}
}
print_r($matches);