XMLDoc GetElementsByTagName 仅第一个条目
XMLDoc GetElementsByTagName Only First Entry
我正在使用 php 创建一个 XML 到 SQL 的文件,但只希望我的脚本处理 XML 文档
中的第一个条目
$xmlObject = $xmlDoc->getElementsByTagName('PlaylistEntry');
正在输入我想要的具体路径
/Playlist/PlaylistEntry[1]/Title[1]/text()
没有产生结果。
有什么建议吗?
编辑:
$xmlDoc = new DOMDocument();
$xmlDoc->load("file.xml");
$mysql_hostname = "";
$mysql_user = "";
$mysql_password = "";
$mysql_database = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Error");
mysql_select_db($mysql_database, $bd) or die("Error");
$xmlObject = $xmlDoc->getElementsByTagName('PlaylistEntry');
$itemCount = $xmlObject->length;
for ($i=0; $i < $itemCount; $i++){
$Title = $xmlObject->item($i)->getElementsByTagName('Title')->item(0)->childNodes->item(0)->nodeValue;
$sql = "INSERT INTO `nowplaying` (Title) VALUES ('$Title')";
mysql_query($sql);
print "Finished Item $Title<br/>";
}
xml:
<Playlist>
<Refresh>41</Refresh>
<PlaylistEntry>
<Title>Tongue Tied</Title>
<Artist>Grouplove</Artist>
<Album>Never Trust A Happy Song</Album>
<ECommerceURL/>
<FileName>02_Tongue_Tied.mp3</FileName>
<trackType/>
<desc/>
<clickThruURL/>
<visualURL>
id=14245829|img=http://upload.wikimedia.org/wikipedia/en/f/f9/GrouploveTongueTied.jpg
</visualURL>
<Seconds>218</Seconds>
</PlaylistEntry>
<PlaylistEntry>
<Title>Holy Roller Novocaine</Title>
<Artist>Kings of Leon</Artist>
<Album>Kings of Leon EP</Album>
<ECommerceURL/>
<FileName>03_Holy_Roller_Novocaine.mp3</FileName>
<trackType/>
<desc/>
<clickThruURL/>
<visualURL>
id=18144083|img=http://upload.wikimedia.org/wikipedia/en/8/86/Holyrollernovacaine.jpg
</visualURL>
<Seconds>257</Seconds>
</PlaylistEntry>
<PlaylistEntry>
...
替换
$xmlObject = $xmlDoc->getElementsByTagName('PlaylistEntry');
$itemCount = $xmlObject->length;
for ($i=0; $i < $itemCount; $i++){
$Title = $xmlObject->item($i)->getElementsByTagName('Title')->item(0)->childNodes->item(0)->nodeValue;
通过此代码使用 xpath
$xpath = new DOMXpath($xmlDoc);
$elements = $xpath->query("/Playlist/PlaylistEntry/Title/text()");
if (!is_null($elements))
foreach ($elements as $element)
$Title = $element->wholeText;
感谢 splash58!
我成功了!
这是我最终得到的结果:
$xpath = new DOMXpath($xmlDoc);
$elements = $xpath->query("/Playlist/PlaylistEntry[1]");
if (!is_null($elements))
foreach ($elements as $element)
$Title = $xpath->query("/Playlist/PlaylistEntry[1]/Title[1]/text()", $element)->item(0)->nodeValue;
$Artist = $xpath->query("/Playlist/PlaylistEntry[1]/Artist[1]/text()", $element)->item(0)->nodeValue;
$sql = "INSERT INTO `nowplaying` (Title, Artist) VALUES ('$Title', '$Artist')";
再次感谢您的帮助:)
我正在使用 php 创建一个 XML 到 SQL 的文件,但只希望我的脚本处理 XML 文档
中的第一个条目$xmlObject = $xmlDoc->getElementsByTagName('PlaylistEntry');
正在输入我想要的具体路径
/Playlist/PlaylistEntry[1]/Title[1]/text()
没有产生结果。
有什么建议吗?
编辑:
$xmlDoc = new DOMDocument();
$xmlDoc->load("file.xml");
$mysql_hostname = "";
$mysql_user = "";
$mysql_password = "";
$mysql_database = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Error");
mysql_select_db($mysql_database, $bd) or die("Error");
$xmlObject = $xmlDoc->getElementsByTagName('PlaylistEntry');
$itemCount = $xmlObject->length;
for ($i=0; $i < $itemCount; $i++){
$Title = $xmlObject->item($i)->getElementsByTagName('Title')->item(0)->childNodes->item(0)->nodeValue;
$sql = "INSERT INTO `nowplaying` (Title) VALUES ('$Title')";
mysql_query($sql);
print "Finished Item $Title<br/>";
}
xml:
<Playlist>
<Refresh>41</Refresh>
<PlaylistEntry>
<Title>Tongue Tied</Title>
<Artist>Grouplove</Artist>
<Album>Never Trust A Happy Song</Album>
<ECommerceURL/>
<FileName>02_Tongue_Tied.mp3</FileName>
<trackType/>
<desc/>
<clickThruURL/>
<visualURL>
id=14245829|img=http://upload.wikimedia.org/wikipedia/en/f/f9/GrouploveTongueTied.jpg
</visualURL>
<Seconds>218</Seconds>
</PlaylistEntry>
<PlaylistEntry>
<Title>Holy Roller Novocaine</Title>
<Artist>Kings of Leon</Artist>
<Album>Kings of Leon EP</Album>
<ECommerceURL/>
<FileName>03_Holy_Roller_Novocaine.mp3</FileName>
<trackType/>
<desc/>
<clickThruURL/>
<visualURL>
id=18144083|img=http://upload.wikimedia.org/wikipedia/en/8/86/Holyrollernovacaine.jpg
</visualURL>
<Seconds>257</Seconds>
</PlaylistEntry>
<PlaylistEntry>
...
替换
$xmlObject = $xmlDoc->getElementsByTagName('PlaylistEntry');
$itemCount = $xmlObject->length;
for ($i=0; $i < $itemCount; $i++){
$Title = $xmlObject->item($i)->getElementsByTagName('Title')->item(0)->childNodes->item(0)->nodeValue;
通过此代码使用 xpath
$xpath = new DOMXpath($xmlDoc);
$elements = $xpath->query("/Playlist/PlaylistEntry/Title/text()");
if (!is_null($elements))
foreach ($elements as $element)
$Title = $element->wholeText;
感谢 splash58!
我成功了!
这是我最终得到的结果:
$xpath = new DOMXpath($xmlDoc);
$elements = $xpath->query("/Playlist/PlaylistEntry[1]");
if (!is_null($elements))
foreach ($elements as $element)
$Title = $xpath->query("/Playlist/PlaylistEntry[1]/Title[1]/text()", $element)->item(0)->nodeValue;
$Artist = $xpath->query("/Playlist/PlaylistEntry[1]/Artist[1]/text()", $element)->item(0)->nodeValue;
$sql = "INSERT INTO `nowplaying` (Title, Artist) VALUES ('$Title', '$Artist')";
再次感谢您的帮助:)