PHP: Check if XML node exists with attribute

前端 未结 4 516
轻奢々
轻奢々 2020-12-06 07:19

I can\'t seem to figure this one out. I have the following XML file:



  
           


        
4条回答
  •  孤街浪徒
    2020-12-06 08:00

    I'd suggest the following (PHP using ext/simplexml and XPath):

    $name = 'Shiny Red';
    $xml = simplexml_load_string('
    
      
        
        
        
      
    ');
    $nodes = $xml->xpath(sprintf('/targets/showcases/building[@name="%s"]', $name);
    if (!empty($nodes)) {
        printf('At least one building named "%s" found', $name);
    } else {
        printf('No building named "%s" found', $name);
    }
    

提交回复
热议问题