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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题