2017-08-01 104 views
-3

我需要獲取值「SONGTITLE」,並用「 - 」分隔它。將XML中的特定值加載到另一個XML中


塊引用

<SHOUTCASTSERVER> 
<CURRENTLISTENERS>2</CURRENTLISTENERS> 
<PEAKLISTENERS>6</PEAKLISTENERS> 
<MAXLISTENERS>5000</MAXLISTENERS> 
<UNIQUELISTENERS>2</UNIQUELISTENERS> 
<AVERAGETIME>1574</AVERAGETIME> 
<SERVERGENRE>Other</SERVERGENRE> 
<SERVERGENRE2/> 
<SERVERGENRE3/> 
<SERVERGENRE4/> 
<SERVERGENRE5/> 
<SERVERURL>https://ww.reyfm.de</SERVERURL> 
<SERVERTITLE>ReyFM | Top100</SERVERTITLE> 
<SONGTITLE> 
Calvin Harris - Feels (feat. Pharrell Williams, Katy Perry & Big Sean) 
</SONGTITLE> 
<SONGURL>DNAS/playingart?sid=1</SONGURL> 
<STREAMHITS>237</STREAMHITS> 
<STREAMSTATUS>1</STREAMSTATUS> 
<BACKUPSTATUS>0</BACKUPSTATUS> 
<STREAMLISTED>0</STREAMLISTED> 
<STREAMLISTEDERROR>200</STREAMLISTEDERROR> 
<STREAMSOURCE>109.230.253.249</STREAMSOURCE> 
<STREAMPATH>/</STREAMPATH> 
<STREAMUPTIME>13511</STREAMUPTIME> 
<BITRATE>320</BITRATE> 
<CONTENT>audio/mpeg</CONTENT> 
<VERSION>2.4.7.256 (posix(linux x64))</VERSION> 
</SHOUTCASTSERVER> 

然後我需要把然後在另一個XML文件:第一個進入「藝術家」,第二個進入「稱號」。例如:

塊引用

<?xml version="1.0" encoding="UTF-8"?> 
<allchannels count="1"> 
<ilr_trackinfo channel="1"> 
<artist> 
<![CDATA[ Post Malone ]]> 
</artist> 
<title> 
<![CDATA[ Congratulations (feat. Quavo) ]]> 
</title> 
<image src="http://95.154.254.129:17618/playingart?sid=1"/> 
</ilr_trackinfo> 
</allchannels> 

「ilr_trackinfo通道=」 1 「」, 「allchannels計數=」 1 「」, 「圖像」 是靜態的。 這可能嗎?


幫助,將不勝感激:)

圖片說明:http://i.stack.imgur.com/KnpYG.png

+1

的問題不是很清楚,但是這似乎找工作XSLT。你可以用PHP或javascript來完成。一些細節會幫助 – Bee157

+0

現在好了嗎? – Nitro

+0

你有什麼選擇:javascript,php,...你在哪種語言中嘗試了一些... – Bee157

回答

0

下面是一個例子:

<?php 

$songs = []; 

$channels = range(1,3); 

// Download current songs (from 1 to 3) 
foreach ($channels as $sid) { 
    $song = file_get_contents(sprintf('http://95.154.254.129:17618/currentsong?sid=%s', $sid)); 
    list($artist, $title) = explode('-', $song, 2); 
    $songs[] = [ 
     'sid' => $sid, 
     'image' => sprintf('http://95.154.254.129:17618/playingart?sid=%s', $sid), 
     'song' => $song, 
     'artist' => trim($artist), 
     'title' => trim($title) 
    ]; 
} 
unset($song); 
//print_r($songs); 

// Write new xml file 
$doc = new DOMDocument('1.0', 'UTF-8'); 
$doc->preserveWhiteSpace = false; 
$doc->formatOutput = true; 

$allChannels = $doc->createElement("allchannels"); 
$allChannels->setAttribute('count' , count($channels)); 
$doc->appendChild($allChannels); 

$trackInfo = $doc->createElement("ilr_trackinfo"); 
$trackInfo->setAttribute('channel' , $song['sid']); 

foreach ($songs as $song) { 
    $trackInfo = $doc->createElement("ilr_trackinfo"); 
    $trackInfo->setAttribute('channel' , $song['sid']); 
    $allChannels->appendChild($trackInfo); 

    $artist = $doc->createElement("artist"); 
    $trackInfo->appendChild($artist); 

    $artistCdata = $doc->createCDATASection($song['artist']); 
    $artist->appendChild($artistCdata); 

    $title = $doc->createElement("title"); 
    $trackInfo->appendChild($title); 

    $titleCdata = $doc->createCDATASection($song['title']); 
    $title->appendChild($titleCdata); 

    $image = $doc->createElement("image"); 
    $image->setAttribute('src', $song['image']); 
    $trackInfo->appendChild($image); 
} 

header("Content-type: text/xml"); 
echo $doc->saveXML(); 
+0

我發現,我可以這樣得到它:http://95.154.254.129:17618/currentsong?sid=1 但我的問題是,我不知道如何將其導入到具有上述格式的新xml文件中。 – Nitro

+0

圖片說明:http://i.stack.imgur.com/KnpYG.png – Nitro

+0

我剛更新了我的答案。 – DanielO

相關問題