PHP script to echo VLC now playing XML attributes

后端 未结 2 1114
醉酒成梦
醉酒成梦 2021-01-25 11:56

I\'ve been searching for a while on this and haven\'t had much luck. I\'ve found plenty of resources showing how to echo data from dynamic XML, but I\'m a PHP novice, and nothi

2条回答
  •  隐瞒了意图╮
    2021-01-25 12:34

    Instead of using a for loop, you can obtain the same result with XPath:

    // Extraction splitted across two lines for clarity
    $artist_xpath = $sxe->xpath('//info[@name="artist"]');
    $artist = (string) $artist_xpath[0];
    echo $artist;
    

    You will have to adjust the xpath expression (i.e. change @name=... appropriately), but you get the idea. Also notice that [0] is necessary because xpath will return an array of matches (and you only need the first) and the cast (string) is used to extract text contained in the node.

    Besides, your XML is invalid and will be rejected by the parser because of the literal & appearing in the tag.

提交回复
热议问题