XML with xpath and PHP: How to access the text value of an attribute of an entry

后端 未结 2 990
情话喂你
情话喂你 2020-12-12 03:43

xml:


  

        
2条回答
  •  孤城傲影
    2020-12-12 04:16

    xpath() returns an array, you have to select the first element of that array, at index 0. Attention: if there's no match, it may return an empty array. Therefore, you should add an if (isset($xpath[0])) clause, just in case.

    foreach ($xml->entry as $entry)
    {
        $xpath = $entry->xpath('./link[@rel="alternate"]/@href');
    
        if (isset($xpath[0]))
        {
            echo $xpath[0], "\n";
        }
    }
    

提交回复
热议问题