douqinlu4217 2012-05-21 08:50
浏览 27
已采纳

如果XML元素不存在,则通过PHP回显消息

After some substantial research on this topic, I have unfortunately not found an answer. I would imagine that this is an exceptionally simple question for a lot of people here.

I'm connected to Last.fm's XML database through their API (Using PHP). On entering an artist that exists, or a blank field, I can echo the correct information with no issues. I cannot however echo anything if the user types in an artist that doesn't exist.

My question is this; how can I echo whatever message I want if that XML element does not exist? My line of thought was as follows:

   foreach ($uk_events as $event){

$venue_city = (string) $event->venue->location->city;
$image = (string) $event->image[2];
$uk_street = (string) $event->venue->location->street;
$uk_postcode = (string) $event->venue->location->postalcode;
$startdate = (string) $event->startDate;
$starttime = (string) $event->startTime;
$uk_venues = (string) $event->venue->name; 
$uk_names = (string) $event->artists->artist;
$website = (string) $event->website;

        if (empty($uk_names)){

    echo "<p class='sorry'>Sorry, but it doesn't look like this artist exists. Either they're exceptionally obscure or they're from
    another realm. Try again, or have a look at the below suggestions.</p>";

    }

After some trial and error the best that I have gotten is for the above to be echoed, but it's echoed regardless of what's searched for. I need the above to appear only if the artist doesn't exist within the XML database.

Much appreciated for anyone who can give me an idea as to how to progress on this one.

Thank you.

  • 写回答

1条回答 默认 最新

  • dongxian4531 2012-05-21 11:35
    关注

    If you're using the SDK from GitHub with the call

    $uk_events = $artistClass->search($methodVars)
    

    then you can edit your code to read

    if ($uk_events === false) {
        echo "<p class='sorry'>Sorry, we couldn't contact the server. Try again later.</p>";             
    
    } elseif (empty($uk_events)) {
        echo "<p class='sorry'>Sorry, but it doesn't look like this artist exists. Either they're exceptionally obscure or they're from another realm. Try again, or have a look at the below suggestions.</p>";             
    
    } else {
       foreach ($uk_events as $event){             
           .... // print list
       }      
    } 
    

    If you're using raw API calls, you'll get a response like

    // Response to http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=NotARealArtist
    
    <?xml version="1.0" encoding="UTF-8"?>
    <lfm status="ok">
      <results xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" for="NotARealArtist">
        <opensearch:Query role="request" startPage="1" searchTerms="NotARealArtist"/>
        <opensearch:totalResults>0</opensearch:totalResults>
        <opensearch:startIndex>0</opensearch:startIndex>
        <opensearch:itemsPerPage>30</opensearch:itemsPerPage>
        <artistmatches>
        </artistmatches>
      </results>
    </lfm>
    

    So, you have two ways of identifying that you have no artists:

    1. the "opensearch:totalResults" element contains a value of "0"
    2. the number of child elements inside "artistmatches" is zero

    So parse the XML for those values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计