0

xmlデータをphpに読み込もうとしています。私はこのsciptですべての「タイトル」データを読み取ることができました。問題は、xmlの最初の「title」要素だけが必要なことです。残念ながら、私はそれらすべてを手に入れました。最初の「タイトル」ノードのみを読み取る方法は?

<?php

$filename = $_SERVER['DOCUMENT_ROOT'].'images/albums/album.xml';
if (file_exists($filename)) {
$doc = new DOMDocument(); 
$doc->load($filename); 
$destinations = $doc->getElementsByTagName("title"); 

foreach ($destinations as $destination) { 
    foreach($destination->childNodes as $child) { 
        if ($child->nodeType == XML_CDATA_SECTION_NODE) { 
            echo ('<br/><strong>Värskemad pildid:</strong><br/><a      href="/images/albums/index.html" src=""/>' . $child->textContent .  '</a>');
        } 
    } 
} 

} else {
die(nothing);
}
?>

私のxmlは次のようになります:

<album version="v6">
<title><![CDATA[2012-06-06 NK maakondlik laager]]></title>

<slide>
    <title><![CDATA[2012 NK maakondlik laager 003]]></title>

<description><![CDATA[text here]]></description>
        <date>05.06.2012 19:12</date>

    <image width="1069" height="813">slides/2012 NK maakondlik laager 003.JPG</image>
    <thumb width="240" height="180">thumbs/2012 NK maakondlik laager 003.JPG</thumb>
<info filesize="3.84 MB" date="05.06.2012 19:12" resolution="3456 x 2592 px" flash="Flash did not fire, auto" focus="5.0mm" 
exposure="1/320s" aperture="3.4" distance="" metering="Center weighted average" 
cameramake="Panasonic" cameramodel="DMC-TZ5" camera="DMC-TZ5" sensor="OneChipColorArea" iso="100"/>
</slide>


<slide>
    <title><![CDATA[2012 NK maakondlik laager 004]]></title>

<description><![CDATA[text here]]></description>
    <date>05.06.2012 19:22</date>

    <image width="1069" height="813">slides/2012 NK maakondlik laager 004.JPG</image>
    <thumb width="240" height="180">thumbs/2012 NK maakondlik laager 004.JPG</thumb>
<info filesize="4.07 MB" date="05.06.2012 19:22" resolution="3456 x 2592 px" flash="Flash did not fire, auto" focus="5.0mm" 
exposure="1/250s" aperture="3.4" distance="" metering="Center weighted average" 
cameramake="Panasonic" cameramodel="DMC-TZ5" camera="DMC-TZ5" sensor="OneChipColorArea" iso="100"/>
</slide>
4

1 に答える 1

0

試す:

<?php
$filename = $_SERVER['DOCUMENT_ROOT'].'images/albums/album.xml';
if (file_exists($filename)) {
$xml = simplexml_load_file($filename);
echo $xml->title[0];
} else {
die("nothing");
}
?>
于 2012-06-24T17:08:05.683 に答える