用PHP解析XML
我对XML没有什么问题。我的XML文件如下所示: 由于此网站不支持xml代码,我无法使用pastebin..:// 所以我需要选择源挂载“/stream”和其他源挂载“/”。我的PHP代码如下所示:用PHP解析XML,php,xml,Php,Xml,我对XML没有什么问题。我的XML文件如下所示: 由于此网站不支持xml代码,我无法使用pastebin..:// 所以我需要选择源挂载“/stream”和其他源挂载“/”。我的PHP代码如下所示: Class stream { Public Function __construct($host, $path, $user, $pass, $mpoint){ $this->host = $host; $this->path = $path;
Class stream {
Public Function __construct($host, $path, $user, $pass, $mpoint){
$this->host = $host;
$this->path = $path;
$this->user = $user;
$this->password = $pass;
$this->mpoint = $mpoint;
$this->url = "http://{$this->user}:{$this->password}@{$this->host}/{$this->path}";
$this->xml = simplexml_load_file($this->url);
}
Public Function audio($url, $format){
return print "<audio controls> <source src=\"{$url}\" type=\"audio/{$format}\"> </audio>";
}
Public Function stream_meta(){
$xml = $this->xml->registerXPathNamespace('test', '/{$this->mpoint}');
$result = $xml->xpath('test:artist');
return $result;
}
Public Function nowplay(){
return $this->xml->source[0]->artist;
}
Public Function download($format){
$link = "http://{$this->host}/{$this->mpoint}";
if($format == "m3u"){
return "{$link}.m3u";
}
elseif($format == "xspf"){
return "{$link}.xspf";
}
elseif($format == "vclt"){
return "{$link}.vclt";
}
return false;
}
}
类流{
公共函数构造($host、$path、$user、$pass、$mpoint){
$this->host=$host;
$this->path=$path;
$this->user=$user;
$this->password=$pass;
$this->mpoint=$mpoint;
$this->url=“http://{$this->user}:{$this->password}@{$this->host}/{$this->path}”;
$this->xml=simplexml\u load\u文件($this->url);
}
公共功能音频($url$格式){
返回打印“”;
}
公共函数流_meta(){
$xml=$this->xml->registerXPathNamespace('test','/{$this->mpoint}');
$result=$xml->xpath('test:artist');
返回$result;
}
公共功能nowplay(){
返回$this->xml->source[0]->artist;
}
公共功能下载($格式){
$link=“http://{$this->host}/{$this->mpoint}”;
如果($format==“m3u”){
返回“{$link}.m3u”;
}
elseif($format==“xspf”){
返回“{$link}.xspf”;
}
elseif($format==“vclt”){
返回“{$link}.vclt”;
}
返回false;
}
}
所以现在我问我做错了什么或者我能做得更好?我需要选择艺术家数据并知道什么是源挂载?这是什么?。我真的需要帮助。我不知道我能做什么,我也没有发明其他的方法来做到这一点。我太累了,我真的需要帮助!请问,有人能帮我吗?我不想使用“nowplay()”函数。因为源挂载“/example”有时会更改…我不完全清楚您想要实现什么,但下面是一个在XML中获取每个源的挂载和艺术家的基本示例:
function sources(){
foreach($this->xml->source as $source) {
echo "Mount: " . $source->attributes()['mount'] . '<br>';
echo "Artist: " . $source->artist . '<br>';
}
}
或者,您也可以在不需要使用循环的情况下执行相同的操作。例如:
function find_source($s){
return $this->xml->xpath('/icestats/source[@mount="'.$s.'"]')[0];
}
谢谢但我怎么才能得到唯一的艺术家呢?不全是,谢谢!我必须去测试这个!在
源代码中,仅针对一名艺术家,或针对mount=“/stream”
?针对mount=“/stream”的艺术家。XML是icecast2服务器的统计信息,我想运行很多流服务器,所以我需要获取统计信息(艺术家,比特率…),所以我告诉source mount=“*/i告诉这个名字*”,然后我得到艺术家的名字。我如何实现这些功能?
function find_source($s){
return $this->xml->xpath('/icestats/source[@mount="'.$s.'"]')[0];
}