将特定值从XML加载到另一个XML中

时间:2017-08-01 16:42:50

标签: javascript php xml

我需要获取值:“SONGTITLE”并将其分隔为“ - ”。

  

块引用

<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 channel =”1“”,“allchannels count =”1“”,“image”是静态的。 这可能吗?

非常感谢帮助:)

图片说明:http://i.stack.imgur.com/KnpYG.png

1 个答案:

答案 0 :(得分: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();