我有一个xml文件。如何制作动态xml?
<?xml version="1.0" encoding="ISO-8859-1"?>
<jukebox>
<song title="When the Levee Breaks" artist="Kansas Joe and Memphis Minnie" url="songs/levee.mp3" />
<song title="Better Leave that Stuff Alone" artist="Will Shade" url="songs/alone.mp3" />
<song title="Walk Right In" artist="Cannon's Jug Stompers" url="songs/walk.mp3" />
</jukebox>
这里我必须从数据库中获取文件。怎么可能?
答案 0 :(得分:1)
这取决于您未详细说明的其他一些因素,但是如果您打算使用 PHP 生成文件,并且您已经整理了数据库逻辑,就像这样会工作:
<?php foreach($songs as $song): ?>
<song title="<?php echo $song['title']; ?>"
artist="<?php echo $song['artist']; ?>"
url="<?php echo $song['url']; ?>" />
<?php endforeach; ?>
更多细节确实是必要的,以便为您提供更多答案。例如,语言?
答案 1 :(得分:1)
我完全不明白你是否想要将这些信息从MySQL中提取到XML或XML到HTML ......
在第一种情况下,我们需要更多信息,例如:
在第二种情况下,您可以使用该代码:
<?php
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<jukebox>
<song title="When the Levee Breaks" artist="Kansas Joe and Memphis Minnie" url="songs/levee.mp3" />
<song title="Better Leave that Stuff Alone" artist="Will Shade" url="songs/alone.mp3" />
<song title="Walk Right In" artist="Cannon\'s Jug Stompers" url="songs/walk.mp3" />
</jukebox>';
$doc = new DOMDocument;
$doc->loadXML($xml);
$songs = $doc->getElementsByTagName('song');
foreach($songs as $song){
echo '<a href="'.$song->getAttribute('url').'">'.$song->getAttribute('title').' by '.$song->getAttribute('artist').'</a>';
}